Skip to content

Commit

Permalink
fix: minor tuning part 1
Browse files Browse the repository at this point in the history
various mild minor changes and comments
  • Loading branch information
mdwmage committed Jun 7, 2024
1 parent d9e0a34 commit de13d5c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 16 deletions.
10 changes: 8 additions & 2 deletions src/entities.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub const Player = struct {
);
}

pub fn listen(self: *Player, beat: *melodie.Beat) f32 {
pub fn listen(self: *Player, beat: *melodie.Beat, level: *Level) f32 {
var moveSpeed: usize = 1;

if (raylib.isKeyDown(raylib.KeyboardKey.key_space)) {
Expand All @@ -37,25 +37,31 @@ pub const Player = struct {
switch (raylib.getKeyPressed()) {
raylib.KeyboardKey.key_w, raylib.KeyboardKey.key_up => {
self.y -= moveSpeed;
level.moves += 1;
return beat.onBeat();
},
raylib.KeyboardKey.key_s, raylib.KeyboardKey.key_down => {
self.y += moveSpeed;
level.moves += 1;
return beat.onBeat();
},
raylib.KeyboardKey.key_a, raylib.KeyboardKey.key_left => {
self.x -= moveSpeed;
level.moves += 1;
return beat.onBeat();
},
raylib.KeyboardKey.key_d, raylib.KeyboardKey.key_right => {
self.x += moveSpeed;
level.moves += 1;
return beat.onBeat();
},
else => {},
}
return -1.0;
}

/// Check & react to players position in relation to tilemap and entities
/// Essentially a "collision" manager
pub fn checkTile(player: *Player, map: *Map, level: *Level) void {
// Check players position based on tiles
switch (map.tiles[player.y][player.x]) {
Expand Down Expand Up @@ -84,7 +90,7 @@ pub const Player = struct {
}
// Check player position based on enemies
switch (map.dungeon[player.y][player.x]) {
Dungeon.empty => {},
Dungeon.empty => {}, // Nothing Happens
Dungeon.item => {
if (player.armed) {} else {
player.armed = true;
Expand Down
1 change: 1 addition & 0 deletions src/game.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub fn loadGame(path: [:0]const u8, allocator: std.mem.Allocator) !screen.Level
// Initialise Level
var game: screen.Level = undefined;
game.complete = false;
game.path = path;
// Load Tilesets
game.tileSet = raylib.Texture2D.init("assets/32rogues/tiles.png");
game.monsterSet = raylib.Texture2D.init("assets/32rogues/monsters.png");
Expand Down
2 changes: 1 addition & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub fn main() !void {
}
},
else => {
currentScene = level.tick();
currentScene = try level.tick(allocator);
},
}
}
Expand Down
67 changes: 54 additions & 13 deletions src/screen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub const MapError = error{
// Structures

pub const Level = struct {
path: [:0]const u8,
player: entities.Player,
map: Map,
tileSet: raylib.Texture2D,
Expand All @@ -32,36 +33,73 @@ pub const Level = struct {
moves: u32,
complete: bool,

pub fn tick(self: *Level) Scene {
/// Batch update all items in a level
pub fn tick(self: *Level, allocator: std.mem.Allocator) !Scene {
// Did the player beat the level?
if (self.complete) {
const returnButton = ui.Button{
.x = 250,
.y = 400,
.y = 550,
.bg = raylib.Color.green,
.fg = raylib.Color.white,
.width = 175,
.height = 40,
.content = "Main Menu?",
};
raylib.clearBackground(raylib.Color.black);
raylib.drawText(raylib.textFormat("Victory! Score: %.1f", .{self.score}), 100, 250, 60, raylib.Color.gold);
// Victory text!
raylib.drawText("Victory!", 50, 250, 60, raylib.Color.gold);
raylib.drawText(
raylib.textFormat("Moves: %d / %d", .{ self.moves, self.map.par }),
50,
315,
60,
raylib.Color.orange,
);
raylib.drawText(
raylib.textFormat("Beat Score: %.1f", .{self.score}),
50,
375,
60,
raylib.Color.pink,
);
// Compute final score
const ratio: f32 = @floatFromInt(self.moves / self.map.par);
var finalScore: f32 = self.score / ratio;
finalScore = @min(9999, finalScore);
raylib.drawText(raylib.textFormat("Final Score: %.0f", .{finalScore}), 50, 430, 60, raylib.Color.lime);
returnButton.render();

// Reset level and return to main menu
if (returnButton.clicked()) {
// Reset level values
self.complete = false;
self.score = 0;
self.moves = 0;
// Stop the music
raylib.stopSound(self.song);
// Reset level & enemies
self.map = (try Map.parse(self.path, allocator)).value;

// Load main menu
return Scene.MainMenu;
}
} else {
}
// Update the game otherwise!
else {
// Music
if (!raylib.isSoundPlaying(self.song)) {
raylib.playSound(self.song);
}

// Tick variables
self.beat.counter += 1;
const accuracy = self.player.listen(&self.beat);
if (accuracy == -1.0) {} else if (accuracy < 0.35) {
self.score = 0;
const accuracy = self.player.listen(&self.beat, self);
self.player.checkTile(&self.map, self);
if (accuracy == -1.0) {
self.score -= 0.01;
} else if (accuracy < 0.35) {
self.score -= 10;
} else {
self.score += accuracy * 100;
}
Expand All @@ -75,20 +113,19 @@ pub const Level = struct {
}

// Render Score

raylib.drawText(raylib.textFormat("Score: %.1f", .{self.score}), 550, 50, 35, raylib.Color.white);

self.player.checkTile(&self.map, self);
}
return Scene.Level;
}
};

pub const Map = struct {
tiles: [][]entities.Tile,
dungeon: [][]entities.Dungeon,
par: u32,
tiles: [][]entities.Tile, // Backdrop
dungeon: [][]entities.Dungeon, // Game entities
par: u32, // Try to keep your moves under this!

/// Draws the map!
/// Cycles through given arrays and draws tiles from the tilemap
pub fn draw(self: *Map, level: Level) void {
var x: f32 = 0;
var y: f32 = 0;
Expand Down Expand Up @@ -165,13 +202,17 @@ pub const Map = struct {
}
}

/// Parses a map from a json file
pub fn parse(path: [:0]const u8, allocator: std.mem.Allocator) !std.json.Parsed(Map) {
// Load level path
var levelPath = try allocator.alloc(u8, path.len + "/level.json".len);
defer allocator.free(levelPath);
std.mem.copyForwards(u8, levelPath[0..], path);
std.mem.copyForwards(u8, levelPath[path.len..], "/level.json");
// Load file to text
const file = try std.fs.cwd().readFileAlloc(allocator, levelPath, 2048);
defer allocator.free(file);
// Attempt to parse the data
const parsedData = try std.json.parseFromSlice(Map, allocator, file, .{ .allocate = .alloc_always });

return parsedData;
Expand Down

0 comments on commit de13d5c

Please sign in to comment.