mirror of
https://github.com/SleepingInsomniac/pixelfaucet
synced 2025-01-20 22:26:58 +01:00
Add animation class
This commit is contained in:
parent
60a9254583
commit
635927465d
4 changed files with 70 additions and 13 deletions
BIN
assets/black-cat.png
Normal file
BIN
assets/black-cat.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 290 B |
|
@ -1,33 +1,29 @@
|
|||
require "../src/game"
|
||||
require "../src/sprite"
|
||||
require "../src/pixel_text"
|
||||
require "../src/animation"
|
||||
|
||||
module PF
|
||||
class SpriteExample < Game
|
||||
@text : PixelText = PixelText.new("assets/pf-font.png")
|
||||
|
||||
@tiles : Array(Sprite)
|
||||
@frame = 0
|
||||
@sub_frame = 0.0
|
||||
@frame_time = 0.1
|
||||
|
||||
def initialize(*args, **kwargs)
|
||||
super
|
||||
@tiles = Sprite.load_tiles("assets/walking.png", 32, 64)
|
||||
@person = Animation.new("assets/walking.png", 32, 64, 10)
|
||||
@cat = Animation.new("assets/black-cat.png", 18, 14, 15)
|
||||
end
|
||||
|
||||
def update(dt, event)
|
||||
@sub_frame += dt
|
||||
if @sub_frame > @frame_time
|
||||
@sub_frame = @sub_frame % @frame_time
|
||||
@frame = (@frame + 1) % @tiles.size
|
||||
end
|
||||
@person.update(dt)
|
||||
@cat.update(dt)
|
||||
end
|
||||
|
||||
def draw
|
||||
clear(60, 120, 200)
|
||||
@text.draw_to(screen, "Frame: #{@frame}", 5, 5)
|
||||
@tiles[@frame].draw_to(screen, (viewport // 2) - @tiles[@frame].size // 2)
|
||||
@text.draw_to(screen, "Frame: #{@person.frame}", 5, 5)
|
||||
fill_rect(0, 65, width - 1, height - 1, Pixel.new(100, 100, 100))
|
||||
@person.draw_to(screen, (viewport // 2) - @person.size // 2)
|
||||
@cat.draw_to(screen, 30, 56)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
39
src/animation.cr
Normal file
39
src/animation.cr
Normal file
|
@ -0,0 +1,39 @@
|
|||
require "./frame_timer"
|
||||
|
||||
module PF
|
||||
class Animation
|
||||
@frames : Array(Sprite)
|
||||
@frame_timer : FrameTimer
|
||||
getter width : Int32
|
||||
getter height : Int32
|
||||
|
||||
def initialize(sheet : String, @width, @height, fps : Int)
|
||||
@frames = Sprite.load_tiles(sheet, width, height)
|
||||
@frame_timer = FrameTimer.new(fps: fps, frame_count: @frames.size)
|
||||
end
|
||||
|
||||
def size
|
||||
current_frame.size
|
||||
end
|
||||
|
||||
def frame
|
||||
@frame_timer.frame
|
||||
end
|
||||
|
||||
def current_frame
|
||||
@frames[frame]
|
||||
end
|
||||
|
||||
def update(dt : Float64)
|
||||
@frame_timer.update(dt)
|
||||
end
|
||||
|
||||
def draw_to(sprite : Sprite, x : Int, y : Int)
|
||||
current_frame.draw_to(sprite, x, y)
|
||||
end
|
||||
|
||||
def draw_to(sprite : Sprite, pos : Vector2(Int))
|
||||
draw_to(sprite, pos.x, pos.y)
|
||||
end
|
||||
end
|
||||
end
|
22
src/frame_timer.cr
Normal file
22
src/frame_timer.cr
Normal file
|
@ -0,0 +1,22 @@
|
|||
module PF
|
||||
# Manage the timing of frames in an animation
|
||||
class FrameTimer
|
||||
getter frame : Int32
|
||||
@frame_time : Float64
|
||||
@frame_count : Int32
|
||||
@sub_frame : Float64 = 0.0
|
||||
|
||||
def initialize(fps : Float64, @frame_count, @frame = 0)
|
||||
@frame_time = 1.0 / fps
|
||||
end
|
||||
|
||||
# Update the timing given a delta time *dt*
|
||||
def update(dt : Float64)
|
||||
@sub_frame += dt
|
||||
if @sub_frame > @frame_time
|
||||
@sub_frame = @sub_frame % @frame_time
|
||||
@frame = (@frame + 1) % @frame_count
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue