From 0d8386099bb5febeaa0e6c31e68b308109080428 Mon Sep 17 00:00:00 2001 From: kdx Date: Fri, 17 Mar 2023 16:38:10 +0100 Subject: [PATCH] jump --- CMakeLists.txt | 1 + src/input.c | 38 ++++++++++++++++++++++++++++++++++++++ src/input.h | 10 ++++++++++ src/main.c | 2 ++ src/map.c | 4 ++-- src/player.c | 25 +++++++++++++++++++++++-- 6 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 src/input.c create mode 100644 src/input.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1937e8c..aa69c35 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,7 @@ find_package(Gint 2.9 REQUIRED) set(SOURCES src/background.c src/entity.c + src/input.c src/game.c src/lzy.c src/main.c diff --git a/src/input.c b/src/input.c new file mode 100644 index 0000000..7235675 --- /dev/null +++ b/src/input.c @@ -0,0 +1,38 @@ +#include "input.h" +#include "lzy.h" + +static const unsigned int keys[6] = {LZYK_LEFT, LZYK_RIGHT, LZYK_UP, + LZYK_DOWN, LZYK_O, LZYK_X}; +static int states[6] = {0}; + +void input_update(void) +{ + int i = 6; + while (i-- > 0) + if (LZY_KeyDown(keys[i])) + states[i] = + (states[i] == KS_UP) ? (KS_PRESSED) : (KS_DOWN); + else + states[i] = KS_UP; +} + +int input_up(unsigned int k) +{ + if (k >= 6) + return 0; + return states[k] == KS_UP; +} + +int input_down(unsigned int k) +{ + if (k >= 6) + return 0; + return states[k] == KS_DOWN || states[k] == KS_PRESSED; +} + +int input_pressed(unsigned int k) +{ + if (k >= 6) + return 0; + return states[k] == KS_PRESSED; +} diff --git a/src/input.h b/src/input.h new file mode 100644 index 0000000..73452a4 --- /dev/null +++ b/src/input.h @@ -0,0 +1,10 @@ +#pragma once + +enum Key { K_LEFT, K_RIGHT, K_UP, K_DOWN, K_O, K_X }; + +enum KeyState { KS_UP, KS_DOWN, KS_PRESSED }; + +void input_update(void); +int input_up(unsigned int); +int input_down(unsigned int); +int input_pressed(unsigned int); diff --git a/src/main.c b/src/main.c index 2c240e0..be96629 100644 --- a/src/main.c +++ b/src/main.c @@ -3,6 +3,7 @@ #include "player.h" #include "background.h" #include "cfg.h" +#include "input.h" #include #include @@ -25,6 +26,7 @@ int main(void) while (!LZY_ShouldQuit()) { LZY_CycleEvents(); + input_update(); game_update(game); LZY_DrawBegin(); diff --git a/src/map.c b/src/map.c index dcc02f7..c82de49 100644 --- a/src/map.c +++ b/src/map.c @@ -12,10 +12,10 @@ static const char *map = "0 0" "0 0" "0 0" - "0 0" + "0 0 0" "00000000000000000000 0" "0 0" - "0 0" + "0 0 0" "0000000000000000000000000" ; diff --git a/src/player.c b/src/player.c index dc60dbb..3787a80 100644 --- a/src/player.c +++ b/src/player.c @@ -3,13 +3,34 @@ #include "game.h" #include "lzy.h" #include "cfg.h" +#include "input.h" #include static void player_update(Entity *this, Game *g) { - this->vel[0] = 1.2; - this->vel[1] += 0.1; + const int on_ground = entity_collide(this, g, 0, 1); + + this->vel[0] = 2.0; + this->vel[1] *= 0.99; + this->vel[1] += 0.2; + + if (on_ground && input_pressed(K_O)) { + const int diry = input_down(K_UP) - input_down(K_DOWN); + switch (diry) { + case -1: + this->vel[1] = -2.8; + break; + default: + case 0: + this->vel[1] = -3.8; + break; + case 1: + this->vel[1] = -4.8; + break; + } + } + entity_move(this, g); }