Allow movement between screens
parent
109b3aade7
commit
6bfead54cc
|
@ -23,6 +23,9 @@ pub const Pos = struct {
|
|||
pub fn getDirection(pos: Pos) Vec2f {
|
||||
return pos.pos - pos.last;
|
||||
}
|
||||
pub fn getVelocity(pos: Pos) Vec2f {
|
||||
return pos.pos - pos.last;
|
||||
}
|
||||
};
|
||||
pub const Control = struct {
|
||||
controller: enum { player },
|
||||
|
|
51
src/game.zig
51
src/game.zig
|
@ -220,16 +220,44 @@ fn loadLevel(lvl: usize) !void {
|
|||
});
|
||||
}
|
||||
|
||||
fn moveLevel(direction: enum { L, R, U, D }, lvl: usize) !void {
|
||||
try loadLevel(lvl);
|
||||
// var momentum = player.pos.pos - player.pos.last;
|
||||
fn moveLevel(direction: enum { L, R, U, D }) !void {
|
||||
// TODO: Figure out the more principled way for checking boundaries
|
||||
var velocity = player.pos.getVelocity();
|
||||
switch (direction) {
|
||||
.L => player.pos.pos[0] = 156,
|
||||
.R => player.pos.pos[0] = 4,
|
||||
.U => player.pos.pos[1] = 156,
|
||||
.D => player.pos.pos[1] = 4,
|
||||
.L => {
|
||||
const x = level.world_x - 1;
|
||||
const y = level.world_y;
|
||||
const lvl = db.findLevel(x, y) orelse return error.NoLevelLeft;
|
||||
|
||||
try loadLevel(lvl);
|
||||
player.pos.pos[0] = 160 - @intToFloat(f32, @divFloor(player.sprite.size[0], 2));
|
||||
},
|
||||
.R => {
|
||||
const x = level.world_x + 1;
|
||||
const y = level.world_y;
|
||||
const lvl = db.findLevel(x, y) orelse return error.NoLevelRight;
|
||||
|
||||
try loadLevel(lvl);
|
||||
player.pos.pos[0] = @intToFloat(f32, @divFloor(player.sprite.size[0], 2));
|
||||
},
|
||||
.U => {
|
||||
const x = level.world_x;
|
||||
const y = level.world_y - 1;
|
||||
const lvl = db.findLevel(x, y) orelse return error.NoLevelUp;
|
||||
|
||||
try loadLevel(lvl);
|
||||
player.pos.pos[1] = 160;
|
||||
},
|
||||
.D => {
|
||||
const x = level.world_x;
|
||||
const y = level.world_y + 1;
|
||||
const lvl = db.findLevel(x, y) orelse return error.NoLevelDown;
|
||||
|
||||
try loadLevel(lvl);
|
||||
player.pos.pos[1] = @intToFloat(f32, player.sprite.size[1]);
|
||||
},
|
||||
}
|
||||
player.pos.last = player.pos.pos;
|
||||
player.pos.last = player.pos.pos - velocity;
|
||||
}
|
||||
|
||||
pub fn start() !void {
|
||||
|
@ -341,7 +369,12 @@ pub fn update(time: usize) !State {
|
|||
physicsProcess(1, &player.pos, &player.physics);
|
||||
try manipulationProcess(&player.pos, &player.control);
|
||||
controlProcess(time, &player.pos, &player.control, &player.physics, &player.kinematic);
|
||||
if (player.pos.pos[0] > 160 - 4) try moveLevel(.R, 1);
|
||||
|
||||
if (player.pos.pos[0] > 160 - 4) try moveLevel(.R);
|
||||
if (player.pos.pos[0] < 4) try moveLevel(.L);
|
||||
if (player.pos.pos[1] > 160 - 4) try moveLevel(.D);
|
||||
if (player.pos.pos[1] < 4) try moveLevel(.U);
|
||||
|
||||
try kinematicProcess(1, &player.pos, &player.kinematic);
|
||||
controlAnimProcess(1, &player.sprite, &player.controlAnim, &player.control);
|
||||
try particles.update();
|
||||
|
|
|
@ -33,7 +33,11 @@ export fn update() void {
|
|||
error.EndOfStream => showErr(@errorName(e)),
|
||||
error.OutOfMemory => showErr(@errorName(e)),
|
||||
error.InvalidLevel => showErr(@errorName(e)),
|
||||
error.NullTiles => showErr(@errorName(e)),
|
||||
error.NullTiles => showErr(@errorName(e)),
|
||||
error.NoLevelDown => showErr(@errorName(e)),
|
||||
error.NoLevelUp => showErr(@errorName(e)),
|
||||
error.NoLevelLeft => showErr(@errorName(e)),
|
||||
error.NoLevelRight => showErr(@errorName(e)),
|
||||
},
|
||||
};
|
||||
if (state != newState) {
|
||||
|
|
|
@ -504,6 +504,15 @@ pub const Database = struct {
|
|||
|
||||
return level_info;
|
||||
}
|
||||
|
||||
pub fn findLevel(db: *Database, x: i8, y: i8) ?usize {
|
||||
for (db.level_info) |level, i| {
|
||||
if (level.x == x and level.y == y) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
// All levels in the game. If two rooms are next to each other, they
|
||||
|
|
Loading…
Reference in New Issue