From 0d9e0c48280e29207d653935d66051e50a2e290e Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Sun, 4 Jun 2023 17:49:04 +0200 Subject: [PATCH] Animating stars (slowly) when not moving as well. --- src/game/animation.zig | 14 ++++++++++---- src/game/game.zig | 4 ++++ src/game_scene.zig | 5 +++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/game/animation.zig b/src/game/animation.zig index 95f844a..e75bf6d 100644 --- a/src/game/animation.zig +++ b/src/game/animation.zig @@ -28,6 +28,15 @@ pub const Animation = struct { return self.sprite.frames[self.current]; } + pub fn currentFrameOffset(self: *Animation, offset: usize) allegro.Bitmap { + return self.sprite.frames[self.currentOffset(offset)]; + } + + pub fn currentOffset(self: Animation, offset: usize) usize { + const n = self.end - self.begin; + return self.begin + (self.current + offset - self.begin) % n; + } + pub fn reset(self: *Animation) void { self.current = self.begin; } @@ -38,9 +47,6 @@ pub const Animation = struct { const skip = @floatToInt(usize, @divFloor(delta, self.interval)); self.lastUpdate = t - @rem(delta, self.interval); - self.current = (self.current + skip); - while (self.current >= self.end) { - self.current = self.begin; - } + self.current = self.currentOffset(skip); } }; diff --git a/src/game/game.zig b/src/game/game.zig index 331d93d..7329d41 100644 --- a/src/game/game.zig +++ b/src/game/game.zig @@ -12,12 +12,15 @@ pub const Game = struct { level: Level, health: i64 = 4, + playerPosition: engine.PointF, playerIdleAnimation: Animation, playerWalkingAnimation: Animation, playerDirection: Direction, isPlayerWalking: bool, + starAnimation: Animation, + renderer: *Renderer, // current viewport translated to tiles. boundsInTiles: engine.RectangleF, @@ -31,6 +34,7 @@ pub const Game = struct { .playerWalkingAnimation = Animation.initPartialLoop(renderer.sprites.get("character_lion_48").?, 0.125, 4, 8), .playerDirection = Direction.Right, .isPlayerWalking = false, + .starAnimation = Animation.init(renderer.sprites.get("item_star_32").?, 0.15), .renderer = renderer, .boundsInTiles = Game.calculateBoundsInTiles(playerPosition), }; diff --git a/src/game_scene.zig b/src/game_scene.zig index 73255e0..9d898d4 100644 --- a/src/game_scene.zig +++ b/src/game_scene.zig @@ -26,7 +26,7 @@ pub const GameScene = struct { } pub fn tick(self: *GameScene, ctx: *Context, t: f32, dt: f32) void { - const speed: f32 = 5; // tiles/s + const speed: f32 = 6; // tiles/s if (ctx.keys.isKeyPressed(allegro.c.ALLEGRO_KEY_LEFT)) { self.game.moveCharacter(-speed * dt); @@ -41,6 +41,7 @@ pub const GameScene = struct { self.game.isPlayerWalking = false; self.game.playerIdleAnimation.tick(t); } + self.game.starAnimation.tick(t); } pub fn render(self: *GameScene, ctx: *Context) void { @@ -90,7 +91,7 @@ pub const GameScene = struct { switch (collectable) { game.Level.Collectable.Star => { const distanceToPlayer = engine.Point.init(x, y).float().distance(self.game.playerPosition); - self.game.drawSpriteFrame("item_star_32", @floatToInt(usize, @mod(distanceToPlayer * 0.54, 1) * 16), x, y); + self.game.drawSpriteFrame("item_star_32", self.game.starAnimation.currentOffset(@floatToInt(usize, @mod(distanceToPlayer * 0.54, 1) * 16)), x, y); }, } }