Animating stars (slowly) when not moving as well.
This commit is contained in:
parent
57f33495d9
commit
0d9e0c4828
@ -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);
|
||||
}
|
||||
};
|
||||
|
@ -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),
|
||||
};
|
||||
|
@ -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);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user