This commit is contained in:
kdx 2023-03-17 16:38:10 +01:00
parent f3f3100dbd
commit 0d8386099b
6 changed files with 76 additions and 4 deletions

View File

@ -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

38
src/input.c Normal file
View File

@ -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;
}

10
src/input.h Normal file
View File

@ -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);

View File

@ -3,6 +3,7 @@
#include "player.h"
#include "background.h"
#include "cfg.h"
#include "input.h"
#include <stdio.h>
#include <stdlib.h>
@ -25,6 +26,7 @@ int main(void)
while (!LZY_ShouldQuit()) {
LZY_CycleEvents();
input_update();
game_update(game);
LZY_DrawBegin();

View File

@ -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"
;

View File

@ -3,13 +3,34 @@
#include "game.h"
#include "lzy.h"
#include "cfg.h"
#include "input.h"
#include <string.h>
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);
}