summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortalha <sarcxd@gmail.com>2025-03-01 21:19:57 +0500
committertalha <sarcxd@gmail.com>2025-03-01 21:19:57 +0500
commitb5dc71d7b4d9e244d76a23cfb59dd782c4c36baf (patch)
tree2be97b85f9c81a3665dc68505df040264cfe1dc7
parent0e0f7d8c2ac664157817221c0f45a1560c271c8b (diff)
Added levels, fixed jumping when gravity is inverted
-rw-r--r--levels/level0.txt23
-rw-r--r--levels/level1.txt16
-rw-r--r--levels/level10.txt15
-rw-r--r--levels/level2.txt13
-rw-r--r--levels/level3.txt12
-rw-r--r--levels/level4.txt10
-rw-r--r--levels/level5.txt24
-rw-r--r--levels/level6.txt14
-rw-r--r--levels/level7.txt20
-rw-r--r--levels/level8.txt11
-rw-r--r--levels/level9.txt11
-rw-r--r--source/array/array.h4
-rwxr-xr-xsource/main.cpp94
-rw-r--r--source/todo.txt3
14 files changed, 212 insertions, 58 deletions
diff --git a/levels/level0.txt b/levels/level0.txt
index 3744ea2..3d3e32e 100644
--- a/levels/level0.txt
+++ b/levels/level0.txt
@@ -1,21 +1,10 @@
# level format
0x1
# type posx posy sizex sizey
- 0 150 70 1 1
- 1 0 0 25 1
- 1 0 0 1 25
- 1 256 128 1 1
- 1 448 256 1 1
- 1 640 384 10 1
- 2 900 800 1 1
- 1 100 1000 25 1
- 1 1500 0 1 25
-# == gameplay elements ==
-# gravity inverter
-# type posx posy sizex sizey
- 3 640 64 1 1
-# teleporter
-# type posx posy sizex sizey id link_id
- 4 832 64 1 2 50 51
- 4 832 448 1 2 51 50
+ 0 192 64 1 1
+ 1 64 0 18 1
+ 1 64 448 18 1
+ 1 64 64 1 6
+ 1 1152 64 1 6
+ 2 960 256 1 1
diff --git a/levels/level1.txt b/levels/level1.txt
index 05364fc..61ea94b 100644
--- a/levels/level1.txt
+++ b/levels/level1.txt
@@ -1,12 +1,12 @@
# level format version number
0x1
# entity_id posx poxy sizex sizey
-1 0 0 20 1
-1 256 128 1 1
-0 128 64 1 1
-1 0 64 1 20
-1 768 1600 10 1
-#.. .. .. .. .. id link_id
-4 768 64 1 2 20 21
-4 1024 1664 1 2 21 20
+0 320 64 1 1
+1 192 0 16 1
+1 192 832 16 1
+1 832 192 5 1
+1 448 384 5 1
+1 192 0 1 14
+1 1152 0 1 14
+2 512 576 1 1
diff --git a/levels/level10.txt b/levels/level10.txt
new file mode 100644
index 0000000..907c6a1
--- /dev/null
+++ b/levels/level10.txt
@@ -0,0 +1,15 @@
+0x1
+1 0 0 44 1
+1 0 64 1 25
+0 128 896 1 1
+2 2688 896 1 1
+1 64 832 8 1
+3 832 576 3 1
+3 1344 1152 3 1
+3 1728 576 3 1
+3 2176 1152 3 1
+1 2368 832 6 1
+1 0 1664 44 1
+1 2752 64 1 25
+3 448 64 1 1
+3 448 1600 1 1
diff --git a/levels/level2.txt b/levels/level2.txt
index 14659f7..8244433 100644
--- a/levels/level2.txt
+++ b/levels/level2.txt
@@ -1,4 +1,11 @@
+# level format version number
0x1
-1 0 0 10 1
-1 256 128 1 1
-0 128 64 1 1
+# entity_id posx poxy sizex sizey
+0 320 64 1 1
+1 192 0 16 1
+1 192 832 16 1
+1 832 192 5 1
+1 192 0 1 14
+1 1152 0 1 14
+2 512 576 1 1
+
diff --git a/levels/level3.txt b/levels/level3.txt
new file mode 100644
index 0000000..329d65a
--- /dev/null
+++ b/levels/level3.txt
@@ -0,0 +1,12 @@
+# level format version number
+0x1
+# entity_id posx poxy sizex sizey
+0 320 64 1 1
+1 192 0 16 1
+1 192 832 16 1
+1 832 192 5 1
+1 832 512 5 1
+1 192 0 1 14
+1 1152 0 1 14
+2 1088 576 1 1
+
diff --git a/levels/level4.txt b/levels/level4.txt
new file mode 100644
index 0000000..c978cfe
--- /dev/null
+++ b/levels/level4.txt
@@ -0,0 +1,10 @@
+0x1
+0 128 64 1 1
+1 0 0 18 1
+1 0 64 1 15
+1 0 960 18 1
+1 1088 64 1 15
+2 256 512 1 1
+3 1024 64 1 1
+1 640 768 3 1
+1 256 640 3 1
diff --git a/levels/level5.txt b/levels/level5.txt
new file mode 100644
index 0000000..abf13a5
--- /dev/null
+++ b/levels/level5.txt
@@ -0,0 +1,24 @@
+0x1
+0 128 64 1 1
+1 0 0 25 1
+1 0 0 1 20
+1 0 1280 25 1
+1 1536 64 1 20
+2 384 1024 1 1
+# stair steps
+1 384 192 3 1
+1 640 320 3 1
+1 896 448 3 1
+1 1152 512 2 1
+1 1152 768 2 1
+1 896 832 3 1
+1 640 960 3 1
+1 384 1088 3 1
+# wall
+1 576 192 1 3
+1 832 320 1 3
+1 1088 448 1 2
+1 1088 768 1 2
+1 832 832 1 3
+1 576 960 1 3
+3 1280 640 1 1
diff --git a/levels/level6.txt b/levels/level6.txt
new file mode 100644
index 0000000..316be51
--- /dev/null
+++ b/levels/level6.txt
@@ -0,0 +1,14 @@
+0x1
+0 128 896 1 1
+1 0 0 23 1
+1 0 0 1 22
+1 0 1408 16 1
+1 64 832 10 1
+1 1408 64 1 8
+2 1344 512 1 1
+1 960 704 1 11
+3 960 640 1 1
+1 960 576 8 1
+3 576 64 1 1
+3 576 1344 1 1
+
diff --git a/levels/level7.txt b/levels/level7.txt
new file mode 100644
index 0000000..4214c98
--- /dev/null
+++ b/levels/level7.txt
@@ -0,0 +1,20 @@
+0x1
+0 128 64 1 1
+1 0 0 30 1
+1 0 0 1 20
+1 0 1280 30 1
+1 1856 64 1 20
+2 896 512 1 1
+# stair steps
+1 384 320 3 1
+1 640 448 3 1
+1 896 576 3 1
+1 1152 704 3 1
+# wall
+1 576 320 1 3
+1 832 448 1 3
+1 1088 576 1 3
+1 1472 640 1 1
+3 1472 512 1 2
+1 1472 448 1 1
+3 1216 1216 1 2
diff --git a/levels/level8.txt b/levels/level8.txt
new file mode 100644
index 0000000..8e065a8
--- /dev/null
+++ b/levels/level8.txt
@@ -0,0 +1,11 @@
+0x1
+0 128 704 1 1
+1 0 0 32 1
+1 0 0 1 20
+1 64 640 10 1
+3 896 448 6 1
+1 1408 640 10 1
+2 1792 576 1 1
+3 576 1216 1 1
+1 0 1280 32 1
+1 2048 0 1 21
diff --git a/levels/level9.txt b/levels/level9.txt
new file mode 100644
index 0000000..b3a91e4
--- /dev/null
+++ b/levels/level9.txt
@@ -0,0 +1,11 @@
+0x1
+1 0 0 16 1
+1 0 0 1 20
+0 128 832 1 1
+1 64 768 8 1
+2 192 704 1 1
+3 448 512 1 1
+1 0 1280 16 1
+1 960 64 1 20
+3 448 1216 1 1
+3 896 64 1 1
diff --git a/source/array/array.h b/source/array/array.h
index cbe710a..6ddf5f0 100644
--- a/source/array/array.h
+++ b/source/array/array.h
@@ -1,15 +1,15 @@
#pragma once
struct r32_array {
- r32 *buffer;
size_t size;
size_t capacity;
+ r32 *buffer;
};
struct u32_array {
- u32 *buffer;
size_t size;
size_t capacity;
+ u32 *buffer;
};
// @r32_array
diff --git a/source/main.cpp b/source/main.cpp
index 437f52f..411229a 100755
--- a/source/main.cpp
+++ b/source/main.cpp
@@ -164,13 +164,23 @@ struct EntityInfoArr {
u32 capacity;
};
+#define ARR_SIZE(arr) (sizeof(arr)/sizeof((arr)[0]))
#define LEVEL_MAX_ENTITIES 100
-const int level_count = 2;
static const char* base_level_path = "./levels/";
-static const char *level_names[20] = {
- "level1.txt",
+static const char *level_names[] = {
"level0.txt",
+ "level1.txt",
+ "level2.txt",
+ "level3.txt",
+ "level4.txt",
+ "level5.txt",
+ "level6.txt",
+ "level7.txt",
+ "level8.txt",
+ "level9.txt",
+ "level10.txt",
};
+const int level_count = ARR_SIZE(level_names);
struct Level0x1 {
u32 version = 0x1;
@@ -719,6 +729,8 @@ int main(int argc, char* argv[])
state.level_path_base = str256(base_level_path);
// @section: gameplay variables
+ u32 jump_count = 1;
+ r64 jump_timer = 0;
r32 motion_scale = 2.0f;
state.gravity_diry = 1.0f;
r32 fall_accelx = 3.0f*motion_scale;
@@ -768,6 +780,8 @@ int main(int argc, char* argv[])
b8 was_colliding = 0;
b8 collidex = 0;
b8 collidey = 0;
+ b8 is_collide_bottom = 0;
+ b8 is_collide_top = 0;
b8 is_gravity = 0;
b8 game_running = 1;
@@ -840,8 +854,8 @@ int main(int argc, char* argv[])
mouse_position_world.x = mouse_position.x + (s32)renderer.cam_pos.x;
mouse_position_world.y = mouse_position.y + (s32)renderer.cam_pos.y;
// clamp mouse position based off of the grids we draw (this will make level object placement easier)
- mouse_position_clamped.x = mouse_position_world.x - (mouse_position_world.x % (s32)(atom_size.x));
- mouse_position_clamped.y = mouse_position_world.y - (mouse_position_world.y % (s32)(atom_size.y));
+ mouse_position_clamped.x = mouse_position_world.x - ((mouse_position_world.x) % (s32)(atom_size.x));
+ mouse_position_clamped.y = mouse_position_world.y - ((mouse_position_world.y) % (s32)(atom_size.y));
} break;
case (SDL_KEYDOWN):
@@ -957,14 +971,6 @@ int main(int argc, char* argv[])
state.effective_force = 0.0f;
p_motion_dir = {0};
}
- if (state.flip_gravity)
- {
- // @resume: I need to add a buffer zone, something like some iframes, so that once I touch a gravity block
- // I don't reflip gravity if I am in contact with the block for 1-2 seconds right after first colliding
- state.gravity_diry = state.gravity_diry > 0.0f ? -0.8f : 1.0f;
- //gravity_diry *= -1.0f;
- state.flip_gravity = 0;
- }
if (controller.move_up)
{
p_move_dir.y = 1.0f;
@@ -996,7 +1002,33 @@ int main(int argc, char* argv[])
is_key_down_x = true;
}
+ if (controller.jump && jump_count > 0 && jump_timer > 100.0f) {
+ controller.jump = 1;
+ jump_count--;
+ jump_timer = 0.0f;
+ } else {
+ controller.jump = 0;
+ }
+
+ // jump increment
+ jump_timer += timer.tDeltaMS;
+ if (is_collide_bottom == 1 && state.gravity_diry > 0.0f) {
+ jump_count = 1;
+ }
+ if (is_collide_top == 1 && state.gravity_diry < 0.0f) {
+ jump_count = 1;
+ }
+
+
// @section: gravity
+ if (state.flip_gravity)
+ {
+ // @resume: I need to add a buffer zone, something like some iframes, so that once I touch a gravity block
+ // I don't reflip gravity if I am in contact with the block for 1-2 seconds right after first colliding
+ state.gravity_diry = state.gravity_diry > 0.0f ? -0.8f : 1.0f;
+ //gravity_diry *= -1.0f;
+ state.flip_gravity = 0;
+ }
Vec2 pd_1 = Vec2{0.0f, 0.0f};
p_motion_dir = {0};
if (collidey)
@@ -1151,6 +1183,8 @@ int main(int argc, char* argv[])
b8 is_collide_x = 0;
b8 is_collide_y = 0;
+ is_collide_bottom = 0;
+ is_collide_top = 0;
for (u32 i = 0; i < state.obstacles.size; i++) {
// @step: check_obstacle_collisions
@@ -1181,20 +1215,24 @@ int main(int argc, char* argv[])
r32 t_right = target.rt.x;
r32 t_top = target.rt.y;
- b8 prev_collide_x = !(prev_left > t_right || prev_right < t_left);
- b8 new_collide_target_top = (p_bottom < t_top && p_top > t_top);
- b8 new_collide_target_bottom = (p_top > t_bottom && p_bottom < t_bottom);
- if (prev_collide_x && new_collide_target_top) {
- t_collide_top = 1;
- }
- if (prev_collide_x && new_collide_target_bottom) {
- t_collide_bottom = 1;
+ if (!is_collide_y) {
+ b8 prev_collide_x = !(prev_left > t_right || prev_right < t_left);
+ b8 new_collide_target_top = (p_bottom < t_top && p_top > t_top);
+ b8 new_collide_target_bottom = (p_top > t_bottom && p_bottom < t_bottom);
+ if (prev_collide_x && new_collide_target_top) {
+ t_collide_top = 1;
+ }
+ if (prev_collide_x && new_collide_target_bottom) {
+ t_collide_bottom = 1;
+ }
}
- b8 prev_collide_y = !(prev_top < t_bottom + 0.2f || prev_bottom > t_top);
- b8 new_collide_x = !(p_right < t_left || p_left > t_right);
- if (prev_collide_y && new_collide_x) {
- t_collide_x = 1;
+ if (1 || is_collide_x) {
+ b8 prev_collide_y = !(prev_top < t_bottom + 0.2f || prev_bottom > t_top);
+ b8 new_collide_x = !(p_right < t_left || p_left > t_right);
+ if (prev_collide_y && new_collide_x) {
+ t_collide_x = 1;
+ }
}
// @func: update_player_positions_if_sides_colliding
@@ -1211,13 +1249,15 @@ int main(int argc, char* argv[])
// (this will collapse various cases where the player is trying to reflip gravity,
// but immediate contact after flipping makes this awkward and infeasible)
if (state.gravity_flip_timer <= 0) {
- state.gravity_flip_timer = 1000.0f;
+ state.gravity_flip_timer = 500.0f;
state.flip_gravity = 1;
}
}
is_collide_x = is_collide_x || t_collide_x;
is_collide_y = is_collide_y || t_collide_top || t_collide_bottom;
+ is_collide_bottom = is_collide_bottom || t_collide_top;
+ is_collide_top = is_collide_top || t_collide_bottom;
}
if (!is_collide_x) {
@@ -1228,7 +1268,7 @@ int main(int argc, char* argv[])
}
// check collision with goal
- if (!is_collide_x && !is_collide_y) {
+ {
Entity goal = state.game_level.entities[state.goal.index];
Rect target = goal.bounds;
diff --git a/source/todo.txt b/source/todo.txt
index aa524cc..ae8fd6b 100644
--- a/source/todo.txt
+++ b/source/todo.txt
@@ -38,7 +38,8 @@ in the level file.
- Port text rendering to stb_truetype
DOING:
+- Making levels
TODO:
-- Making levels
+- add panning from level goal to player position when loading a new level