Rendering different orientations of a tile.

This commit is contained in:
Sander Schobers 2023-06-04 20:47:55 +02:00
parent 7a51415e5e
commit 219eebbfb6
6 changed files with 63 additions and 17 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

After

Width:  |  Height:  |  Size: 4.8 KiB

View File

@ -14,7 +14,7 @@ pub const Context = struct {
palette: Palette = undefined,
shouldQuit: bool = false,
showFPS: bool = true,
showDebug: bool = true,
showDebug: bool = false,
scene: ?Scene = null,
events: allegro.EventQueue,

View File

@ -114,7 +114,7 @@ pub const Sprite = struct {
var i: i32 = 0;
while (i < n) : (i += 1) {
const left = @mod(i, horizontal) * width;
const top = @divTrunc(i, horizontal) * width;
const top = @divTrunc(i, horizontal) * height;
frames[@intCast(usize, i)] = try bitmap.sub(left, top, width, height);
}

View File

@ -27,6 +27,22 @@ pub fn TileMap(comptime Value: type) type {
}
};
pub const Ordinals = struct {
left: bool,
top: bool,
right: bool,
bottom: bool,
pub fn count(self: Ordinals) usize {
var n: usize = 0;
if (self.left) n += 1;
if (self.top) n += 1;
if (self.right) n += 1;
if (self.bottom) n += 1;
return n;
}
};
allocator: std.mem.Allocator,
columns: std.AutoHashMap(i64, Column),
@ -64,6 +80,24 @@ pub fn TileMap(comptime Value: type) type {
return null;
}
pub fn getOrdinals(self: Self, x: i64, y: i64, ofType: 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),
};
}
pub fn isOfType(self: Self, x: i64, y: i64, ofType: Value) bool {
if (self.get(x, y)) |value| {
if (value == ofType) {
return true;
}
}
return false;
}
pub fn set(self: *Self, x: i64, y: i64, value: Value) !void {
const c = try self.ensureColumn(x);
try c.set(y, value);

View File

@ -50,11 +50,7 @@ pub const GameScene = struct {
const renderer = ctx.renderer;
const viewport = renderer.viewport;
const center = viewport.center();
_ = center;
const bounds = viewport.bounds;
const scale = viewport.scale;
_ = scale;
const background = renderer.textures.get("background_jungle").?;
const backgroundDisplacementFactor = -0.01;
@ -66,30 +62,46 @@ pub const GameScene = struct {
const tileBounds = self.game.tilesInView();
const tiles = self.game.level.tiles;
const collectables = self.game.level.collectables;
var x = tileBounds.min.x;
while (x < tileBounds.max.x) : (x += 1) {
const randomDirtOffset = self.game.randomTileOffset(x, 99, 3);
self.game.drawSpriteFrame("tiles_dirt_32", 0 + randomDirtOffset, x, 19);
self.game.drawSpriteFrame("tiles_dirt_32", 3 + randomDirtOffset, x, 20);
if (x < 0) continue;
const tiles = self.game.level.tiles.ensureColumn(x) catch continue;
const collectables = self.game.level.collectables.ensureColumn(x) catch continue;
var y = tileBounds.min.y;
while (y < tileBounds.max.y) : (y += 1) {
if (tiles.get(y)) |tile| {
while (y <= tileBounds.max.y) : (y += 1) {
if (tiles.get(x, y)) |tile| {
switch (tile) {
game.Level.Tile.Grass => {
const randomOffset = self.game.randomTileOffset(x, y, 2);
self.game.drawSpriteFrame("tiles_grass_32", 1 + randomOffset, x, y - 1);
self.game.drawSpriteFrame("tiles_grass_32", 5 + randomOffset, x, y);
const ordinals = tiles.getOrdinals(x, y, game.Level.Tile.Grass);
var offset: usize = 17;
switch (ordinals.count()) {
1 => {
if (ordinals.right) offset = 0;
if (ordinals.left) offset = 3;
},
2 => {
if (ordinals.right and ordinals.left) offset = 1 + self.game.randomTileOffset(x, y, 2);
if (ordinals.top and ordinals.bottom) offset = 12;
if (ordinals.right and ordinals.bottom) offset = 4;
if (ordinals.left and ordinals.bottom) offset = 5;
},
3 => {
if (!ordinals.left) offset = 6;
if (!ordinals.right) offset = 7;
},
else => {},
}
self.game.drawSpriteFrame("tiles_grass_32", offset, x, y - 1);
},
// else => {},
}
}
if (collectables.get(y)) |collectable| {
if (collectables.get(x, y)) |collectable| {
switch (collectable) {
game.Level.Collectable.Star => {
const distanceToPlayer = engine.Point.init(x, y).float().distance(self.game.playerPosition);

View File

@ -50,7 +50,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, "tiles_dirt_32", 32, 32);
try renderer.sprites.addFromTextures(renderer.textures, "tiles_grass_32", 32, 32);
try renderer.sprites.addFromTextures(renderer.textures, "tiles_grass_32", 32, 64);
allegro.convertMemoryBitmaps();