Added game over.
This commit is contained in:
parent
eab51541bf
commit
7f2719df43
@ -3,19 +3,19 @@ x
|
||||
x x
|
||||
x S S x
|
||||
x xxxxxx S S x
|
||||
x xxxxxxxxxxx x
|
||||
x xxxxxxxxxxx x
|
||||
x x
|
||||
x xxxxx x
|
||||
x xxxxxx x x
|
||||
x x xxxxxxxxx x
|
||||
x S S x x x
|
||||
x xxxxxx xxxxxx xxxxx xxxxx xxxxxxx xxxxx x
|
||||
x x x x
|
||||
x x x xxx x
|
||||
x x S x x x x
|
||||
xxxxxxxxxx xxxxx xxxxx x xxxx xxx x x
|
||||
x x x x x
|
||||
x S S S x xxx x x
|
||||
x P S S S S S x x x x
|
||||
x x xxxxxxxxx x
|
||||
x S S x x x
|
||||
x xxxxxx xxxxxx xxxxx xxxxx xxxxxxx xxxxx x
|
||||
x x x x
|
||||
x x x xxx x
|
||||
x x S x x x S S x
|
||||
xxxxxxxxxx xxxxx xxxxx x xxxx xxx x xxxxx xxxxx x
|
||||
x x x x x
|
||||
x S S S x xxx x x
|
||||
x P S S S S S x x x E x
|
||||
xxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||||
|
@ -83,6 +83,7 @@ pub const Context = struct {
|
||||
const viewport = self.renderer.viewport;
|
||||
const fonts = &self.renderer.fonts;
|
||||
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("large", paths.AssetsDir ++ "/fonts/Pixellari.ttf", viewport.scaledInteger(64));
|
||||
try fonts.addFromFileTTF("debug", paths.AssetsDir ++ "/fonts/Cabin-Regular.ttf", viewport.scaledInteger(16));
|
||||
|
@ -17,7 +17,7 @@ pub const Game = struct {
|
||||
prng: std.rand.DefaultPrng = std.rand.DefaultPrng.init(0),
|
||||
|
||||
level: Level,
|
||||
health: i64 = 4,
|
||||
isOver: bool = false,
|
||||
starsCollected: usize = 0,
|
||||
|
||||
playerPosition: engine.PointF,
|
||||
@ -85,8 +85,16 @@ pub const Game = struct {
|
||||
}
|
||||
|
||||
pub fn movePlayer(self: *Game, dt: f32) void {
|
||||
if (self.isOver) return;
|
||||
|
||||
const to = self.playerPosition.add(self.playerVelocity.multiply(dt));
|
||||
self.playerPosition = to;
|
||||
|
||||
if (self.playerPosition.y > 21.5) {
|
||||
self.isOver = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (self.playerVelocity.y > 0 and self.playerIsOnTile()) {
|
||||
self.playerPosition.y = std.math.floor(self.playerPosition.y);
|
||||
self.playerVelocity.y = 0;
|
||||
|
@ -10,6 +10,10 @@ pub const GameScene = struct {
|
||||
game: game.Game = undefined,
|
||||
|
||||
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;
|
||||
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 {
|
||||
_ = event;
|
||||
_ = ctx;
|
||||
_ = self;
|
||||
if (self.game.isOver) {
|
||||
switch (event.type) {
|
||||
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 {
|
||||
@ -32,6 +50,11 @@ pub const GameScene = struct {
|
||||
const maxFallVelocity: f32 = 23;
|
||||
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)) {
|
||||
self.game.playerVelocity.x = std.math.max(-maxHorizontalWalkVelocity, self.game.playerVelocity.x - horizontalWalkAcceleration);
|
||||
} 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 {
|
||||
allegro.clearToColor(ctx.palette.background.background);
|
||||
|
||||
const renderer = ctx.renderer;
|
||||
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});
|
||||
|
||||
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) {
|
||||
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 });
|
||||
|
@ -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, "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_grass_32", 32, 64);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user