Rendering different orientations of a tile.
This commit is contained in:
parent
7a51415e5e
commit
219eebbfb6
Binary file not shown.
Before Width: | Height: | Size: 3.0 KiB After Width: | Height: | Size: 4.8 KiB |
@ -14,7 +14,7 @@ pub const Context = struct {
|
|||||||
palette: Palette = undefined,
|
palette: Palette = undefined,
|
||||||
shouldQuit: bool = false,
|
shouldQuit: bool = false,
|
||||||
showFPS: bool = true,
|
showFPS: bool = true,
|
||||||
showDebug: bool = true,
|
showDebug: bool = false,
|
||||||
scene: ?Scene = null,
|
scene: ?Scene = null,
|
||||||
|
|
||||||
events: allegro.EventQueue,
|
events: allegro.EventQueue,
|
||||||
|
@ -114,7 +114,7 @@ pub const Sprite = struct {
|
|||||||
var i: i32 = 0;
|
var i: i32 = 0;
|
||||||
while (i < n) : (i += 1) {
|
while (i < n) : (i += 1) {
|
||||||
const left = @mod(i, horizontal) * width;
|
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);
|
frames[@intCast(usize, i)] = try bitmap.sub(left, top, width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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,
|
allocator: std.mem.Allocator,
|
||||||
columns: std.AutoHashMap(i64, Column),
|
columns: std.AutoHashMap(i64, Column),
|
||||||
|
|
||||||
@ -64,6 +80,24 @@ pub fn TileMap(comptime Value: type) type {
|
|||||||
return null;
|
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 {
|
pub fn set(self: *Self, x: i64, y: i64, value: Value) !void {
|
||||||
const c = try self.ensureColumn(x);
|
const c = try self.ensureColumn(x);
|
||||||
try c.set(y, value);
|
try c.set(y, value);
|
||||||
|
@ -50,11 +50,7 @@ pub const GameScene = struct {
|
|||||||
const renderer = ctx.renderer;
|
const renderer = ctx.renderer;
|
||||||
const viewport = renderer.viewport;
|
const viewport = renderer.viewport;
|
||||||
|
|
||||||
const center = viewport.center();
|
|
||||||
_ = center;
|
|
||||||
const bounds = viewport.bounds;
|
const bounds = viewport.bounds;
|
||||||
const scale = viewport.scale;
|
|
||||||
_ = scale;
|
|
||||||
const background = renderer.textures.get("background_jungle").?;
|
const background = renderer.textures.get("background_jungle").?;
|
||||||
const backgroundDisplacementFactor = -0.01;
|
const backgroundDisplacementFactor = -0.01;
|
||||||
|
|
||||||
@ -66,30 +62,46 @@ pub const GameScene = struct {
|
|||||||
|
|
||||||
const tileBounds = self.game.tilesInView();
|
const tileBounds = self.game.tilesInView();
|
||||||
|
|
||||||
|
const tiles = self.game.level.tiles;
|
||||||
|
const collectables = self.game.level.collectables;
|
||||||
|
|
||||||
var x = tileBounds.min.x;
|
var x = tileBounds.min.x;
|
||||||
while (x < tileBounds.max.x) : (x += 1) {
|
while (x < tileBounds.max.x) : (x += 1) {
|
||||||
const randomDirtOffset = self.game.randomTileOffset(x, 99, 3);
|
const randomDirtOffset = self.game.randomTileOffset(x, 99, 3);
|
||||||
self.game.drawSpriteFrame("tiles_dirt_32", 0 + randomDirtOffset, x, 19);
|
self.game.drawSpriteFrame("tiles_dirt_32", 0 + randomDirtOffset, x, 19);
|
||||||
self.game.drawSpriteFrame("tiles_dirt_32", 3 + randomDirtOffset, x, 20);
|
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;
|
var y = tileBounds.min.y;
|
||||||
while (y < tileBounds.max.y) : (y += 1) {
|
while (y <= tileBounds.max.y) : (y += 1) {
|
||||||
if (tiles.get(y)) |tile| {
|
if (tiles.get(x, y)) |tile| {
|
||||||
switch (tile) {
|
switch (tile) {
|
||||||
game.Level.Tile.Grass => {
|
game.Level.Tile.Grass => {
|
||||||
const randomOffset = self.game.randomTileOffset(x, y, 2);
|
const ordinals = tiles.getOrdinals(x, y, game.Level.Tile.Grass);
|
||||||
self.game.drawSpriteFrame("tiles_grass_32", 1 + randomOffset, x, y - 1);
|
var offset: usize = 17;
|
||||||
self.game.drawSpriteFrame("tiles_grass_32", 5 + randomOffset, x, y);
|
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 => {},
|
// else => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (collectables.get(y)) |collectable| {
|
if (collectables.get(x, y)) |collectable| {
|
||||||
switch (collectable) {
|
switch (collectable) {
|
||||||
game.Level.Collectable.Star => {
|
game.Level.Collectable.Star => {
|
||||||
const distanceToPlayer = engine.Point.init(x, y).float().distance(self.game.playerPosition);
|
const distanceToPlayer = engine.Point.init(x, y).float().distance(self.game.playerPosition);
|
||||||
|
@ -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, "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, "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, 32);
|
try renderer.sprites.addFromTextures(renderer.textures, "tiles_grass_32", 32, 64);
|
||||||
|
|
||||||
allegro.convertMemoryBitmaps();
|
allegro.convertMemoryBitmaps();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user