Added collection of stars.
This commit is contained in:
parent
c7930db414
commit
eab51541bf
@ -15,7 +15,7 @@ pub const Context = struct {
|
||||
allocator: std.mem.Allocator,
|
||||
palette: Palette = undefined,
|
||||
shouldQuit: bool = false,
|
||||
showFPS: bool = true,
|
||||
showFPS: bool = false,
|
||||
showDebug: bool = false,
|
||||
scene: ?Scene = null,
|
||||
|
||||
@ -82,9 +82,9 @@ pub const Context = struct {
|
||||
pub fn registerFonts(self: *Context) !void {
|
||||
const viewport = self.renderer.viewport;
|
||||
const fonts = &self.renderer.fonts;
|
||||
try fonts.addFromFileTTF("default", paths.AssetsDir ++ "/fonts/Pixellari.ttf", viewport.scaledInteger(16));
|
||||
try fonts.addFromFileTTF("default", paths.AssetsDir ++ "/fonts/Pixellari.ttf", viewport.scaledInteger(32));
|
||||
try fonts.addFromFileTTF("extra-small", paths.AssetsDir ++ "/fonts/Pixellari.ttf", viewport.scaledInteger(12));
|
||||
try fonts.addFromFileTTF("large", paths.AssetsDir ++ "/fonts/Pixellari.ttf", viewport.scaledInteger(32));
|
||||
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));
|
||||
}
|
||||
|
||||
|
@ -18,6 +18,7 @@ pub const Game = struct {
|
||||
|
||||
level: Level,
|
||||
health: i64 = 4,
|
||||
starsCollected: usize = 0,
|
||||
|
||||
playerPosition: engine.PointF,
|
||||
playerFallingAnimation: Animation,
|
||||
@ -30,6 +31,7 @@ pub const Game = struct {
|
||||
|
||||
renderer: *Renderer,
|
||||
randomTile: TileMap(usize),
|
||||
collected: TileMap(bool),
|
||||
// current viewport translated to tiles.
|
||||
boundsInTiles: engine.RectangleF,
|
||||
|
||||
@ -46,6 +48,7 @@ pub const Game = struct {
|
||||
.starAnimation = Animation.init(renderer.sprites.get("item_star_32").?, 0.15),
|
||||
.renderer = renderer,
|
||||
.randomTile = TileMap(usize).init(allocator),
|
||||
.collected = TileMap(bool).init(allocator),
|
||||
.boundsInTiles = Game.calculateBoundsInTiles(playerPosition),
|
||||
};
|
||||
}
|
||||
@ -111,6 +114,29 @@ pub const Game = struct {
|
||||
}
|
||||
self.playerDirection = if (self.playerVelocity.x == 0) self.playerDirection else if (self.playerVelocity.x > 0) Direction.Right else Direction.Left;
|
||||
|
||||
const playerCenter = self.playerPosition.add(engine.PointF.init(0.5, 0.5)).floor().integer();
|
||||
var dx: i64 = -1;
|
||||
while (dx <= 1) : (dx += 1) {
|
||||
var dy: i64 = -1;
|
||||
while (dy <= 1) : (dy += 1) {
|
||||
const x = playerCenter.x + dx;
|
||||
const y = playerCenter.y + dy;
|
||||
const collectablePosition = engine.Point.init(x, y);
|
||||
|
||||
if (self.level.collectables.get(x, y)) |collectable| {
|
||||
if (self.collected.hasValue(x, y, true)) continue;
|
||||
if (self.playerPosition.distance2(collectablePosition.float()) > 1.5) continue;
|
||||
|
||||
switch (collectable) {
|
||||
Level.Collectable.Star => {
|
||||
self.starsCollected += 1;
|
||||
self.collected.set(x, y, true) catch {};
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.boundsInTiles = Game.calculateBoundsInTiles(self.playerPosition);
|
||||
}
|
||||
|
||||
|
@ -18,6 +18,16 @@ pub fn TileMap(comptime Value: type) type {
|
||||
self.values.deinit();
|
||||
}
|
||||
|
||||
pub fn countValue(self: Column, value: Value) usize {
|
||||
var count: usize = 0;
|
||||
while (self.values.valueIterator()) |v| {
|
||||
if (value == v) {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
pub fn get(self: Column, row: i64) ?Value {
|
||||
return self.values.get(row);
|
||||
}
|
||||
@ -65,6 +75,14 @@ pub fn TileMap(comptime Value: type) type {
|
||||
return self.columns.getPtr(x).?;
|
||||
}
|
||||
|
||||
pub fn countValue(self: Self, value: Value) usize {
|
||||
var count: usize = 0;
|
||||
var columns = self.columns.valueIterator();
|
||||
while (columns.next()) |c| {
|
||||
count += c.countValue(value);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ensureColumn(self: *Self, x: i64) !*Column {
|
||||
if (self.columns.getPtr(x)) |c| {
|
||||
return c;
|
||||
@ -80,18 +98,18 @@ pub fn TileMap(comptime Value: type) type {
|
||||
return null;
|
||||
}
|
||||
|
||||
pub fn getOrdinals(self: Self, x: i64, y: i64, ofType: Value) Ordinals {
|
||||
pub fn getOrdinals(self: Self, x: i64, y: i64, value: Value) Ordinals {
|
||||
return Ordinals{
|
||||
.left = self.isOfType(x - 1, y, ofType),
|
||||
.top = self.isOfType(x, y - 1, ofType),
|
||||
.right = self.isOfType(x + 1, y, ofType),
|
||||
.bottom = self.isOfType(x, y + 1, ofType),
|
||||
.left = self.hasValue(x - 1, y, value),
|
||||
.top = self.hasValue(x, y - 1, value),
|
||||
.right = self.hasValue(x + 1, y, value),
|
||||
.bottom = self.hasValue(x, y + 1, value),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn isOfType(self: Self, x: i64, y: i64, ofType: Value) bool {
|
||||
if (self.get(x, y)) |value| {
|
||||
if (value == ofType) {
|
||||
pub fn hasValue(self: Self, x: i64, y: i64, value: Value) bool {
|
||||
if (self.get(x, y)) |v| {
|
||||
if (value == v) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -132,6 +132,8 @@ pub const GameScene = struct {
|
||||
}
|
||||
|
||||
if (collectables.get(x, y)) |collectable| {
|
||||
if (self.game.collected.hasValue(x, y, true)) continue;
|
||||
|
||||
switch (collectable) {
|
||||
game.Level.Collectable.Star => {
|
||||
const distanceToPlayer = engine.Point.init(x, y).float().distance(self.game.playerPosition);
|
||||
@ -151,6 +153,7 @@ pub const GameScene = struct {
|
||||
self.game.drawSpriteFrameP("character_lion_48", self.game.playerIdleAnimation.current + playerDirectionFrameOffset, self.game.playerPosition.add(engine.PointF.init(-0.25, -0.25)));
|
||||
}
|
||||
|
||||
ctx.renderer.printTextV("default", ctx.palette.background.text, 0.01, 0.01, Renderer.TextAlignment.Left, "Stars collected: {d}", .{self.game.starsCollected});
|
||||
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 });
|
||||
|
@ -41,6 +41,8 @@ pub fn main() !void {
|
||||
try renderer.textures.addFromFile("opaque", paths.AssetsDir ++ "/images/opaque.png");
|
||||
try renderer.textures.addFromFile("title_untitled", paths.AssetsDir ++ "/images/title_untitled.png");
|
||||
try renderer.textures.addFromFile("background_jungle", paths.AssetsDir ++ "/images/background_jungle.png");
|
||||
|
||||
allegro.setNewBitmapFlags(allegro.NewBitmapFlags{ .MIN_LINEAR = false, .MAG_LINEAR = false });
|
||||
try renderer.textures.addFromFile("character_lion_48", paths.AssetsDir ++ "/images/character_lion_48.png");
|
||||
try renderer.textures.addFromFile("item_star_32", paths.AssetsDir ++ "/images/item_star_32.png");
|
||||
try renderer.textures.addFromFile("text_balloons", paths.AssetsDir ++ "/images/text_balloons.png");
|
||||
@ -108,7 +110,7 @@ pub fn main() !void {
|
||||
|
||||
scene.render(&context);
|
||||
if (context.showFPS) {
|
||||
renderer.printTextV("debug", context.palette.background.text, 0.01, 0.01, Renderer.TextAlignment.Left, "FPS: {}", .{context.fps.fps()});
|
||||
renderer.printTextV("debug", context.palette.background.text, 0.99, 0.01, Renderer.TextAlignment.Right, "FPS: {}", .{context.fps.fps()});
|
||||
}
|
||||
|
||||
allegro.flipDisplay();
|
||||
|
Loading…
Reference in New Issue
Block a user