Added game over.

This commit is contained in:
Sander Schobers 2023-06-05 03:59:25 +02:00
parent eab51541bf
commit 7f2719df43
5 changed files with 60 additions and 17 deletions

View File

@ -3,19 +3,19 @@ x
x x x x
x S S x x S S x
x xxxxxx S S x x xxxxxx S S x
x xxxxxxxxxxx x x xxxxxxxxxxx x
x x x x
x xxxxx x x xxxxx x
x xxxxxx x x x xxxxxx x x
x x xxxxxxxxx x x x xxxxxxxxx x
x S S x x x x S S x x x
x xxxxxx xxxxxx xxxxx xxxxx xxxxxxx xxxxx x x xxxxxx xxxxxx xxxxx xxxxx xxxxxxx xxxxx x
x x x x x x x x
x x x xxx x x x x xxx x
x x S x x x x x x S x x x S S x
xxxxxxxxxx xxxxx xxxxx x xxxx xxx x x xxxxxxxxxx xxxxx xxxxx x xxxx xxx x xxxxx xxxxx x
x x x x x x x x x x
x S S S x xxx x x x S S S x xxx x x
x P S S S S S x x x x x P S S S S S x x x E x
xxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

View File

@ -83,6 +83,7 @@ pub const Context = struct {
const viewport = self.renderer.viewport; const viewport = self.renderer.viewport;
const fonts = &self.renderer.fonts; const fonts = &self.renderer.fonts;
try fonts.addFromFileTTF("default", paths.AssetsDir ++ "/fonts/Pixellari.ttf", viewport.scaledInteger(32)); try fonts.addFromFileTTF("default", paths.AssetsDir ++ "/fonts/Pixellari.ttf", viewport.scaledInteger(32));
try fonts.addFromFileTTF("small", paths.AssetsDir ++ "/fonts/Pixellari.ttf", viewport.scaledInteger(16));
try fonts.addFromFileTTF("extra-small", paths.AssetsDir ++ "/fonts/Pixellari.ttf", viewport.scaledInteger(12)); try fonts.addFromFileTTF("extra-small", paths.AssetsDir ++ "/fonts/Pixellari.ttf", viewport.scaledInteger(12));
try fonts.addFromFileTTF("large", paths.AssetsDir ++ "/fonts/Pixellari.ttf", viewport.scaledInteger(64)); try fonts.addFromFileTTF("large", paths.AssetsDir ++ "/fonts/Pixellari.ttf", viewport.scaledInteger(64));
try fonts.addFromFileTTF("debug", paths.AssetsDir ++ "/fonts/Cabin-Regular.ttf", viewport.scaledInteger(16)); try fonts.addFromFileTTF("debug", paths.AssetsDir ++ "/fonts/Cabin-Regular.ttf", viewport.scaledInteger(16));

View File

@ -17,7 +17,7 @@ pub const Game = struct {
prng: std.rand.DefaultPrng = std.rand.DefaultPrng.init(0), prng: std.rand.DefaultPrng = std.rand.DefaultPrng.init(0),
level: Level, level: Level,
health: i64 = 4, isOver: bool = false,
starsCollected: usize = 0, starsCollected: usize = 0,
playerPosition: engine.PointF, playerPosition: engine.PointF,
@ -85,8 +85,16 @@ pub const Game = struct {
} }
pub fn movePlayer(self: *Game, dt: f32) void { pub fn movePlayer(self: *Game, dt: f32) void {
if (self.isOver) return;
const to = self.playerPosition.add(self.playerVelocity.multiply(dt)); const to = self.playerPosition.add(self.playerVelocity.multiply(dt));
self.playerPosition = to; self.playerPosition = to;
if (self.playerPosition.y > 21.5) {
self.isOver = true;
return;
}
if (self.playerVelocity.y > 0 and self.playerIsOnTile()) { if (self.playerVelocity.y > 0 and self.playerIsOnTile()) {
self.playerPosition.y = std.math.floor(self.playerPosition.y); self.playerPosition.y = std.math.floor(self.playerPosition.y);
self.playerVelocity.y = 0; self.playerVelocity.y = 0;

View File

@ -10,6 +10,10 @@ pub const GameScene = struct {
game: game.Game = undefined, game: game.Game = undefined,
pub fn enter(self: *GameScene, ctx: *Context) void { pub fn enter(self: *GameScene, ctx: *Context) void {
self.load(ctx);
}
fn load(self: *GameScene, ctx: *Context) void {
const level = game.Level.init(ctx.allocator, paths.AssetsDir ++ "/levels/level1.txt") catch unreachable; const level = game.Level.init(ctx.allocator, paths.AssetsDir ++ "/levels/level1.txt") catch unreachable;
self.game = game.Game.init(ctx.allocator, level, &ctx.renderer); self.game = game.Game.init(ctx.allocator, level, &ctx.renderer);
} }
@ -20,9 +24,23 @@ pub const GameScene = struct {
} }
pub fn handle(self: *GameScene, ctx: *Context, event: *allegro.Event) !void { pub fn handle(self: *GameScene, ctx: *Context, event: *allegro.Event) !void {
_ = event; if (self.game.isOver) {
_ = ctx; switch (event.type) {
_ = self; allegro.c.ALLEGRO_EVENT_KEY_DOWN => {
switch (event.keyboard.keycode) {
allegro.c.ALLEGRO_KEY_ESCAPE => {
ctx.quit();
},
allegro.c.ALLEGRO_KEY_ENTER => {
self.game.deinit();
self.load(ctx);
},
else => {},
}
},
else => {},
}
}
} }
pub fn tick(self: *GameScene, ctx: *Context, t: f32, dt: f32) void { pub fn tick(self: *GameScene, ctx: *Context, t: f32, dt: f32) void {
@ -32,6 +50,11 @@ pub const GameScene = struct {
const maxFallVelocity: f32 = 23; const maxFallVelocity: f32 = 23;
const maxHorizontalWalkVelocity: f32 = 7; // tiles/s const maxHorizontalWalkVelocity: f32 = 7; // tiles/s
if (self.game.isOver) {
self.game.playerIdleAnimation.tick(t);
return;
}
if (ctx.keys.isKeyPressed(allegro.c.ALLEGRO_KEY_LEFT)) { if (ctx.keys.isKeyPressed(allegro.c.ALLEGRO_KEY_LEFT)) {
self.game.playerVelocity.x = std.math.max(-maxHorizontalWalkVelocity, self.game.playerVelocity.x - horizontalWalkAcceleration); self.game.playerVelocity.x = std.math.max(-maxHorizontalWalkVelocity, self.game.playerVelocity.x - horizontalWalkAcceleration);
} else if (ctx.keys.isKeyPressed(allegro.c.ALLEGRO_KEY_RIGHT)) { } else if (ctx.keys.isKeyPressed(allegro.c.ALLEGRO_KEY_RIGHT)) {
@ -70,8 +93,6 @@ pub const GameScene = struct {
} }
pub fn render(self: *GameScene, ctx: *Context) void { pub fn render(self: *GameScene, ctx: *Context) void {
allegro.clearToColor(ctx.palette.background.background);
const renderer = ctx.renderer; const renderer = ctx.renderer;
const viewport = renderer.viewport; const viewport = renderer.viewport;
@ -154,6 +175,18 @@ pub const GameScene = struct {
} }
ctx.renderer.printTextV("default", ctx.palette.background.text, 0.01, 0.01, Renderer.TextAlignment.Left, "Stars collected: {d}", .{self.game.starsCollected}); ctx.renderer.printTextV("default", ctx.palette.background.text, 0.01, 0.01, Renderer.TextAlignment.Left, "Stars collected: {d}", .{self.game.starsCollected});
if (self.game.isOver) {
const textColor = allegro.mapRgb(0x48, 0x91, 0x00);
ctx.renderer.textures.get("opaque").?.drawTintedScaled(allegro.mapRgba(0, 0, 0, 191), 0, 0, @intToFloat(f32, ctx.renderer.display.width()), @intToFloat(f32, ctx.renderer.display.height()));
ctx.renderer.drawTextV("default", textColor, 0.5, 0.2, .Center, "Game over");
ctx.renderer.drawSpriteFrameV("text_balloons", 0, 0.48, 0.40);
ctx.renderer.drawTextV("small", ctx.palette.background.text, 0.547, 0.436, .Center, "Do you want to");
ctx.renderer.drawTextV("small", ctx.palette.background.text, 0.547, 0.465, .Center, "try again?");
ctx.renderer.drawSpriteFrameV("character_lion_48", self.game.playerIdleAnimation.current + 12, 0.44, 0.57);
ctx.renderer.drawTextV("small", textColor, 0.5, 0.7, .Center, "[enter] try again | [escape] quit");
}
if (ctx.showDebug) { if (ctx.showDebug) {
ctx.renderer.printTextV("debug", ctx.palette.background.text, 0.01, 0.1, Renderer.TextAlignment.Left, "Character: ({d:.2}, {d:.2})", .{ self.game.playerPosition.x, self.game.playerPosition.y }); ctx.renderer.printTextV("debug", ctx.palette.background.text, 0.01, 0.1, Renderer.TextAlignment.Left, "Character: ({d:.2}, {d:.2})", .{ self.game.playerPosition.x, self.game.playerPosition.y });
ctx.renderer.printTextV("debug", ctx.palette.background.text, 0.01, 0.15, Renderer.TextAlignment.Left, "Tiles: ({d}, {d}) -> ({d}, {d})", .{ tileBounds.min.x, tileBounds.min.y, tileBounds.max.x, tileBounds.max.y }); ctx.renderer.printTextV("debug", ctx.palette.background.text, 0.01, 0.15, Renderer.TextAlignment.Left, "Tiles: ({d}, {d}) -> ({d}, {d})", .{ tileBounds.min.x, tileBounds.min.y, tileBounds.max.x, tileBounds.max.y });

View File

@ -51,6 +51,7 @@ pub fn main() !void {
try renderer.sprites.addFromTextures(renderer.textures, "character_lion_48", 48, 48); try renderer.sprites.addFromTextures(renderer.textures, "character_lion_48", 48, 48);
try renderer.sprites.addFromTextures(renderer.textures, "item_star_32", 32, 32); try renderer.sprites.addFromTextures(renderer.textures, "item_star_32", 32, 32);
try renderer.sprites.addFromTextures(renderer.textures, "text_balloons", 128, 96);
try renderer.sprites.addFromTextures(renderer.textures, "tiles_dirt_32", 32, 32); try renderer.sprites.addFromTextures(renderer.textures, "tiles_dirt_32", 32, 32);
try renderer.sprites.addFromTextures(renderer.textures, "tiles_grass_32", 32, 64); try renderer.sprites.addFromTextures(renderer.textures, "tiles_grass_32", 32, 64);