Skip to content

Commit 36b8032

Browse files
committed
Make ladders good, down is not crawl
Going down and up ladders with left stick is now working well. In modern input mode, analog stick down is not crawl anymore. This allows crawling on top of ladders without going down the ladders.
1 parent ff83639 commit 36b8032

File tree

6 files changed

+97
-53
lines changed

6 files changed

+97
-53
lines changed

src/_debug.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// This file is for various debug feature flags
22

33
pub const player_position: bool = false;
4+
pub const player_action: bool = false;
5+
pub const ladder_flag: bool = false;
46
pub const controller_osd: bool = false;
57
pub const dump_sprites: bool = false;

src/globals.zig

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,16 @@ pub var GRANDBRULE_FLAG: bool = false;
6666
pub var LADDER_FLAG: bool = false;
6767

6868
//True if player is forced into kneestanding because of low ceiling
69-
pub var PRIER_FLAG: bool = false;
69+
pub var low_ceiling: bool = false;
7070

71-
//6 if free fall or in the middle of a jump, decremented if on solid surface. Must be 0 to initiate a jump.
72-
pub var SAUT_FLAG: u8 = 0;
71+
// 6 if free fall or in the middle of a jump, decremented if on solid surface. Must be 0 to initiate a jump.
72+
pub var jump_timer: u8 = 0;
73+
74+
// Incremented from 0 to 3 when accelerating while jumping, stop acceleration upwards if >= 3
75+
pub var jump_acceleration_counter: u8 = 0;
76+
77+
// Player is falling? (0 = no, 1 = yes, 2 = )
78+
pub var YFALL: u8 = 0;
7379

7480
//Last action (kneestand + jump = silent walk)
7581
pub var LAST_ORDER: player.PlayerAction = .Rest;
@@ -102,8 +108,6 @@ pub var GRAVITY_FLAG: u8 = 0;
102108
//Smoke when object hits the floor
103109
pub var FUME_FLAG: u8 = 0;
104110

105-
pub var YFALL: u8 = 0;
106-
107111
pub var POCKET_FLAG: bool = false;
108112

109113
//Increased every loop in game loop
@@ -142,9 +146,6 @@ pub var IMAGE_COUNTER: u16 = 0;
142146
//1: walk right, 0: stand still, -1: walk left, triggers the ACTION_TIMER if it changes
143147
pub var SENSX: i8 = 0;
144148

145-
//Incremented from 0 to 3 when accelerating while jumping, stop acceleration upwards if >= 3
146-
pub var SAUT_COUNT: u8 = 0;
147-
148149
pub var NOSCROLL_FLAG: bool = false;
149150

150151
//Finish a level

src/input.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ fn handle_gamepad(pad_state: *GamepadState) void {
727727
}
728728
}
729729

730-
if(pad_state.dpad_down_pressed or pad_state.left_shoulder_pressed or pad_state.left_trigger > 0.0)
730+
if(pad_state.dpad_down_pressed)
731731
{
732732
// crawl
733733
g_input_state.y_axis = 1;

src/player.zig

Lines changed: 71 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ pub const PlayerAction = enum(u8) {
103103
},
104104
};
105105
}
106-
fn withoutCarry(self: *PlayerAction) PlayerAction {
107-
return switch(self.*) {
106+
fn withoutCarry(self: PlayerAction) PlayerAction {
107+
return switch(self) {
108108
.Rest_Carry => .Rest,
109109
.Walk_Carry => .Walk,
110110
.Jump_Carry => .Jump,
@@ -120,10 +120,31 @@ pub const PlayerAction = enum(u8) {
120120
.Hit_Carry => .Hit,
121121
.HitBurn_Carry => .HitBurn,
122122
else => {
123-
return self.*;
123+
return self;
124124
},
125125
};
126126
}
127+
128+
pub fn str(self: PlayerAction) []const u8 {
129+
const no_carry = self.withoutCarry();
130+
return switch(no_carry) {
131+
.Rest => "Rest",
132+
.Walk => "Walk",
133+
.Jump => "Jump",
134+
.Crawl => "Crawl",
135+
.Slide => "Slide",
136+
.KneeStand => "KneeStand",
137+
.Climb => "Climb",
138+
.Grab => "Grab",
139+
.Drop => "Drop",
140+
.SilentRest => "SRest",
141+
.SilentWalk => "SWalk",
142+
.Headache => "Headache",
143+
.Hit => "Hit",
144+
.HitBurn => "HitBurn",
145+
else => "Unknown"
146+
};
147+
}
127148
};
128149

129150
pub fn move_player(arg_context: *render.ScreenContext, arg_level: *lvl.Level) c_int {
@@ -187,12 +208,12 @@ pub fn move_player(arg_context: *render.ScreenContext, arg_level: *lvl.Level) c_
187208
globals.GRANDBRULE_FLAG = false;
188209
if (globals.LADDER_FLAG) {
189210
action = .Climb;
190-
} else if (!globals.PRIER_FLAG and player.jump_pressed and player.y_axis <= 0 and globals.SAUT_FLAG == 0) {
211+
} else if (!globals.low_ceiling and player.jump_pressed and player.y_axis <= 0 and globals.jump_timer == 0) {
191212
action = .Jump;
192213
if (globals.LAST_ORDER == .KneeStand) { // Test if last order was kneestanding
193214
globals.FURTIF_FLAG = 100; // If jump after kneestanding, init silent walk timer
194215
}
195-
} else if (globals.PRIER_FLAG or (globals.SAUT_FLAG != 6 and player.y_axis > 0)) {
216+
} else if (globals.low_ceiling or (globals.jump_timer != 6 and player.crouch_pressed)) {
196217
if (player.x_axis != 0) { // Move left or right
197218
action = .Crawl; // Action: crawling
198219
} else {
@@ -204,7 +225,7 @@ pub fn move_player(arg_context: *render.ScreenContext, arg_level: *lvl.Level) c_
204225
action = .Rest; // Action: rest (no action)
205226
}
206227
// Is space button pressed?
207-
if (player.action_pressed and !globals.PRIER_FLAG) {
228+
if (player.action_pressed and !globals.low_ceiling) {
208229
if (!globals.DROP_FLAG) {
209230
if (action == .Crawl or action == .KneeStand) { // Kneestand
210231
globals.DROPREADY_FLAG = false;
@@ -480,7 +501,7 @@ fn TAKE_BLK_AND_YTEST(level: *lvl.Level, tileY_in: i16, tileX_in: i16) void {
480501
var tileX = tileX_in;
481502
const player = &level.*.player;
482503
globals.POCKET_FLAG = false;
483-
globals.PRIER_FLAG = false;
504+
globals.low_ceiling = false;
484505
globals.LADDER_FLAG = false;
485506
var change: i8 = undefined;
486507
// if player is too high (<= -1), skip test
@@ -552,12 +573,12 @@ fn BLOCK_YYPRGD(level: *lvl.Level, cflag: lvl.CeilingType, tileY_in: i16, tileX_
552573
// Stop movement
553574
player.sprite.speed_y = 0;
554575
player.sprite.y = @as(i16, @bitCast(@as(u16, @bitCast(player.sprite.y)) & 0xFFF0)) + 16;
555-
globals.SAUT_COUNT = 0xFF;
576+
globals.jump_acceleration_counter = 0xFF;
556577
} else if (player.sprite.number != 10 and // 10 = Free fall
557578
player.sprite.number != 21 and // 21 = Free fall (c)
558-
globals.SAUT_FLAG != 6)
579+
globals.jump_timer != 6)
559580
{
560-
globals.PRIER_FLAG = true;
581+
globals.low_ceiling = true;
561582
if (globals.CARRY_FLAG) {
562583
const maybe_object = player_drop_carried(level);
563584
if (maybe_object) |object| {
@@ -582,7 +603,7 @@ fn BLOCK_YYPRGD(level: *lvl.Level, cflag: lvl.CeilingType, tileY_in: i16, tileX_
582603
//if (player.sprite.speed_y < 0 and player.sprite.speed_x == 0) {
583604
//if (player.sprite.speed_y <= 0 and player.y_axis < 0 and player.x_axis == 0) {
584605
if (player.sprite.speed_y <= 0 and player.y_axis < 0) {
585-
globals.SAUT_COUNT = 10;
606+
globals.jump_acceleration_counter = 10;
586607
globals.LADDER_FLAG = true;
587608
}
588609
},
@@ -625,7 +646,7 @@ fn player_block_x(level: *lvl.Level) void {
625646
// Horizontal hit (wall), stop the player
626647
player.sprite.x -= player.sprite.speed_x >> 4;
627648
player.sprite.speed_x = 0;
628-
if (globals.KICK_FLAG != 0 and globals.SAUT_FLAG != 6) {
649+
if (globals.KICK_FLAG != 0 and globals.jump_timer != 6) {
629650
globals.CHOC_FLAG = 20;
630651
globals.KICK_FLAG = 0;
631652
}
@@ -666,7 +687,7 @@ pub fn player_drop_carried(level: *lvl.Level) ?*lvl.Object {
666687
fn player_fall(level: *lvl.Level) void {
667688
// No wall under the player; fall down!
668689
const player = &level.player;
669-
globals.SAUT_FLAG = 6;
690+
globals.jump_timer = 6;
670691
if (globals.KICK_FLAG != 0) {
671692
return;
672693
}
@@ -747,30 +768,36 @@ fn BLOCK_YYPRG(level: *lvl.Level, floor: lvl.FloorType, floor_above: lvl.FloorTy
747768
return;
748769
}
749770
const order = globals.LAST_ORDER.withoutCarry();
771+
const is_in_air = globals.YFALL != 0 or globals.jump_acceleration_counter != 0;
750772
// Skip if walking/crawling
751-
if (order == .Walk or order == .Crawl or order == .Grab or order == .Drop) {
752-
player_block_yu(player); // Stop fall
753-
return;
754-
}
755-
// Crawl
756-
if (order == .KneeStand) { // action baisse
757-
player_fall_F(); // Free fall
758-
sprites.updatesprite(level, &player.sprite, 14, true); // sprite: start climbing down
759-
player.sprite.y += 8;
760-
}
761-
if (floor_above != .Ladder) { // ladder
762-
if (order == .Rest) { // action repos
773+
if (order == .Walk or order == .Crawl or order == .Grab or order == .Drop or order == .KneeStand or order == .Rest) {
774+
if(player.y_axis <= 0 and !is_in_air) {
763775
player_block_yu(player); // Stop fall
764776
return;
765777
}
778+
else
779+
{
780+
player_fall_F(); // Free fall
781+
const flip = player.sprite.flipped;
782+
sprites.updatesprite(level, &player.sprite, 14, true); // sprite: start climbing down
783+
player.sprite.flipped = flip;
784+
player.sprite.y += 8;
785+
}
786+
}
787+
// we stand on top of a ladder
788+
if (floor_above != .Ladder) { // ladder
789+
// if (order == .Rest) { // action repos
790+
// player_block_yu(player); // Stop fall
791+
// return;
792+
// }
766793
if (player.y_axis < 0 and order == .Climb) { // action UP + climb ladder
767794
player_block_yu(player); // Stop fall
768795
return;
769796
}
770797
}
771798

772-
common.subto0(&globals.SAUT_FLAG);
773-
globals.SAUT_COUNT = 0;
799+
common.subto0(&globals.jump_timer);
800+
globals.jump_acceleration_counter = 0;
774801
globals.YFALL = 2;
775802

776803
globals.LADDER_FLAG = true;
@@ -811,8 +838,8 @@ fn player_block_yu(player: *lvl.Player) void {
811838
}
812839
player.sprite.y = @bitCast(@as(u16, @bitCast(player.sprite.y)) & 0xFFF0);
813840
player.sprite.speed_y = 0;
814-
common.subto0(&globals.SAUT_FLAG);
815-
globals.SAUT_COUNT = 0;
841+
common.subto0(&globals.jump_timer);
842+
globals.jump_acceleration_counter = 0;
816843
globals.YFALL = 2;
817844
}
818845

@@ -916,13 +943,13 @@ fn ACTION_PRG(level: *lvl.Level, action: PlayerAction) void {
916943
},
917944
.Jump, .Jump_Carry => {
918945
// Handle a jump
919-
if (globals.SAUT_COUNT == 0) {
946+
if (globals.jump_acceleration_counter == 0) {
920947
events.triggerEvent(.Event_PlayerJump);
921948
}
922-
if (globals.SAUT_COUNT >= 3) {
923-
globals.SAUT_FLAG = 6; // Stop jump animation and acceleration
949+
if (globals.jump_acceleration_counter >= 3) {
950+
globals.jump_timer = 6; // Stop jump animation and acceleration
924951
} else {
925-
globals.SAUT_COUNT +%= 1;
952+
globals.jump_acceleration_counter +%= 1;
926953
YACCELERATION_NEG(player, globals.MAX_Y * 16 / 4);
927954
XACCELERATION(player, globals.MAX_X * 16);
928955
NEW_FORM(player, action);
@@ -964,6 +991,13 @@ fn ACTION_PRG(level: *lvl.Level, action: PlayerAction) void {
964991
sprites.updatesprite(level, &player.sprite, 23, true); // First climb sprite (c)
965992
}
966993
}
994+
const is_in_air = globals.YFALL != 0 or globals.jump_acceleration_counter != 0;
995+
// attach to ladder in air
996+
if(is_in_air and globals.LAST_ORDER != .Climb) {
997+
NEW_FORM(player, if (globals.CARRY_FLAG) .Climb_Carry else .Climb);
998+
GET_IMAGE(level);
999+
player.sprite.speed_y = 0;
1000+
}
9671001
if (player.y_axis != 0) {
9681002
NEW_FORM(player, if (globals.CARRY_FLAG) .Climb_Carry else .Climb);
9691003
GET_IMAGE(level);
@@ -1342,8 +1376,8 @@ fn player_collide_with_elevators(level: *lvl.Level) void {
13421376
player.sprite.y = elevator.sprite.y;
13431377

13441378
player.sprite.speed_y = 0;
1345-
common.subto0(&globals.SAUT_FLAG);
1346-
globals.SAUT_COUNT = 0;
1379+
common.subto0(&globals.jump_timer);
1380+
globals.jump_acceleration_counter = 0;
13471381
globals.YFALL = 2;
13481382

13491383
player.sprite.x += elevator.sprite.speed_x;
@@ -1438,8 +1472,8 @@ fn player_collide_with_objects(level: *lvl.Level) void {
14381472
} else {
14391473
player.sprite.speed_y = 0;
14401474
}
1441-
common.subto0(&globals.SAUT_FLAG);
1442-
globals.SAUT_COUNT = 0;
1475+
common.subto0(&globals.jump_timer);
1476+
globals.jump_acceleration_counter = 0;
14431477
globals.YFALL = 2;
14441478
}
14451479
}

src/render.zig

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,24 @@ pub fn render_sprites(level: *lvl.Level) void {
137137
if (debug.player_position) {
138138
const x = level.player.sprite.x - (globals.BITMAP_X * 16) + globals.g_scroll_px_offset;
139139
const y = level.player.sprite.y - (globals.BITMAP_Y * 16) + get_y_offset();
140-
_ = SDL.writeSurfacePixel(window.screen, 20, 10, 255, 0, 0, 255);
141-
if(!SDL.writeSurfacePixel(window.screen, x, y, 255, 0, 0, 255)) {
142-
std.log.err("Can't write pixel: {s}!", .{SDL.getError()} );
140+
//_ = SDL.writeSurfacePixel(window.screen, 20, 10, 255, 0, 0, 255);
141+
if(y >= 0 and x >= 0 and y < window.game_height and x < window.game_width) {
142+
_ = SDL.writeSurfacePixel(window.screen, x, y, 255, 0, 0, 255);
143143
}
144-
145144
var buf = [_]u8{0} ** 32;
146145
const bytes = std.fmt.bufPrint(&buf, "{d},{d}", .{level.player.sprite.x >> 4, level.player.sprite.y >> 4}) catch {
147146
unreachable;
148147
};
149148
fonts.Gold.render(bytes, 30 * 8, 0 * 12, .{ .monospace = true });
150149
}
150+
if (debug.ladder_flag) {
151+
if(globals.LADDER_FLAG) {
152+
fonts.Gold.render("LADDER", 30 * 8, 1 * 12, .{ .monospace = true });
153+
}
154+
}
155+
if (debug.player_action) {
156+
fonts.Gold.render(globals.LAST_ORDER.str(), 30 * 8, 2 * 12, .{ .monospace = true });
157+
}
151158

152159
if (globals.GODMODE) {
153160
fonts.Gold.render("GODMODE", 30 * 8, 0 * 12, .{ .monospace = true });

src/reset.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ pub fn CLEAR_DATA(level: *lvl.Level) void {
6464
globals.DROPREADY_FLAG = false;
6565
globals.POSEREADY_FLAG = false;
6666
globals.LADDER_FLAG = false;
67-
globals.PRIER_FLAG = false;
68-
globals.SAUT_FLAG = 0;
67+
globals.low_ceiling = false;
68+
globals.jump_timer = 0;
6969
globals.CROSS_FLAG = 0;
7070
globals.FURTIF_FLAG = 0;
7171
globals.CHOC_FLAG = 0;
@@ -78,7 +78,7 @@ pub fn CLEAR_DATA(level: *lvl.Level) void {
7878
globals.SKIPLEVEL_FLAG = false;
7979
globals.INVULNERABLE_FLAG = 0;
8080
globals.POCKET_FLAG = false;
81-
globals.SAUT_COUNT = 0;
81+
globals.jump_acceleration_counter = 0;
8282
globals.ACTION_TIMER = 0;
8383
globals.g_scroll_x = false;
8484
globals.g_scroll_y = false;

0 commit comments

Comments
 (0)