summaryrefslogtreecommitdiff
path: root/src/player.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/player.c')
-rw-r--r--src/player.c57
1 files changed, 45 insertions, 12 deletions
diff --git a/src/player.c b/src/player.c
index 0bff092..6a671f0 100644
--- a/src/player.c
+++ b/src/player.c
@@ -1,27 +1,51 @@
#include "player.h"
+#include "conf.h"
#include "input.h"
+#include "level.h"
#include "lzy.h"
-static float x, y, spd_x, spd_y;
+static int x, y;
+static float spd_y;
+
+static int collide_wall(void);
void player_init(float nx, float ny)
{
x = nx;
- y = ny;
- spd_x = 0.0f;
- spd_y = 0.0f;
+ y = ny + TILE_SIZE - PLAYER_HEIGHT;
+ spd_y = 0.00000002022f;
}
void player_update(void)
{
- if (input_down(K_LEFT))
- x -= 2;
- if (input_down(K_RIGHT))
- x += 2;
- if (input_down(K_UP))
- y -= 2;
- if (input_down(K_DOWN))
- y += 2;
+ x += input_down(K_RIGHT) * 2 - input_down(K_LEFT) * 3;
+ if (collide_wall())
+ x -= input_down(K_RIGHT) * 3 - input_down(K_LEFT) * 4;
+
+ y++;
+ if (collide_wall()) {
+ if (input_pressed(K_O))
+ spd_y = JUMP_SPEED;
+ } else {
+ spd_y += GRAVITY;
+ if (spd_y > MAX_Y_SPEED)
+ spd_y = MAX_Y_SPEED;
+ }
+ y--;
+
+ y += spd_y;
+ if (collide_wall()) {
+ const int step = (MAX_Y_SPEED * (-1 + 2 * (spd_y > 0))) / 2;
+ y -= step;
+ if (collide_wall())
+ y -= step;
+ spd_y = 0.00000001337;
+ }
+
+ if (level_at(x, y) == 3)
+ level_next();
+ if (level_at(x, y) == 4)
+ level_reload();
}
void player_draw(void)
@@ -29,3 +53,12 @@ void player_draw(void)
LZY_DrawSetColor(255, 0, 255);
LZY_DrawTile(2, x, y);
}
+
+static int collide_wall(void)
+{
+ const int x2 = x + PLAYER_WIDTH - 1;
+ const int y2 = y + PLAYER_HEIGHT - 1;
+
+ return level_at(x, y) == 1 || level_at(x2, y) == 1 ||
+ /* level_at(x, y2) == 1 || */ level_at(x2, y2) == 1;
+}