acceleration and friction

This commit is contained in:
Masséna Fezard | Nounouille 2021-12-23 10:00:55 +01:00
parent e136461f63
commit c7c961cc60
5 changed files with 62 additions and 8 deletions

View File

@ -15,6 +15,7 @@ set(SOURCES
src/draw.c
src/player.c
src/input.c
src/tools.c
)
set(ASSETS_cg

View File

@ -1,3 +1,9 @@
#pragma once
#define PLAYER_S 12
#define PLAYER_ACC 1
#define PLAYER_FRIC 0.2
#define PLAYER_JUMP 7
#define PLAYER_COYOTE 3
#define GRAVITY 0.4

3
include/tools.h Normal file
View File

@ -0,0 +1,3 @@
#pragma once
int sign(int x);

View File

@ -2,11 +2,13 @@
#include "conf.h"
#include "draw.h"
#include "input.h"
#include "tools.h"
static int k_up, k_down, k_left, k_right, k_jump;
static struct Player player;
static void player_get_input(void);
static void player_move(void);
static void player_move(struct Vec2 mov);
static int player_collide(void);
void
player_init(void)
@ -21,10 +23,7 @@ player_update(void)
player_get_input();
const struct Vec2 mov = (struct Vec2){k_right - k_left, k_down - k_up};
player.spd.x = mov.x;
player.spd.y = mov.y;
player_move();
player_move(mov);
}
void
@ -44,8 +43,46 @@ player_get_input(void)
}
static void
player_move(void)
player_move(struct Vec2 mov)
{
player.pos.x += player.spd.x;
player.pos.y += player.spd.y;
if (player_collide()) {
return;
}
player.spd.x *= (1 - PLAYER_FRIC);
player.spd.x += mov.x * PLAYER_ACC;
player.spd.y *= (1 - PLAYER_FRIC);
player.spd.y += mov.y * PLAYER_ACC;
if (!player.spd.x && !player.spd.y) {
return;
}
const int sign_x = sign(player.spd.x);
const int Ispd_x = player.spd.x;
player.pos.x += Ispd_x;
if (player_collide()) {
player.spd.x = 0.0f;
while (player_collide()) {
player.pos.x -= sign_x;
}
}
const int sign_y = sign(player.spd.y);
const int Ispd_y = player.spd.y;
player.pos.y += Ispd_y;
if (player_collide()) {
player.spd.y = 0.0f;
while (player_collide()) {
player.pos.y -= sign_y;
}
}
}
static int
player_collide(void)
{
/* Je ne fais qu'acte de présence, rien de plus */
return 0;
}

7
src/tools.c Normal file
View File

@ -0,0 +1,7 @@
#include "tools.h"
int
sign(int x)
{
return (x > 0) - (x < 0); /* from jtmm2 */
}