hyperultra/src/player.c

88 lines
2.1 KiB
C
Raw Normal View History

2023-03-17 10:51:17 +01:00
#include "player.h"
#include "entity.h"
#include "game.h"
2023-03-17 10:57:19 +01:00
#include "lzy.h"
2023-03-17 13:45:20 +01:00
#include "cfg.h"
2023-03-17 16:38:10 +01:00
#include "input.h"
2023-03-17 10:51:17 +01:00
#include <string.h>
2023-03-17 17:00:47 +01:00
#include <math.h>
2023-03-17 10:51:17 +01:00
static void
player_update(Entity *this, Game *g)
{
2023-03-17 16:38:10 +01:00
const int on_ground = entity_collide(this, g, 0, 1);
2023-03-17 17:00:47 +01:00
this->vel[0] = 2.0 * this->player.dirx;
2023-03-17 16:38:10 +01:00
this->vel[1] *= 0.99;
this->vel[1] += 0.2;
2023-03-17 16:54:07 +01:00
this->player.scale_x += 0.1 * (1.0 - this->player.scale_x);
this->player.scale_y += 0.1 * (1.0 - this->player.scale_y);
if (fabs(this->player.scale_x - 1.0) < 0.05) this->player.scale_x = 1.01;
if (fabs(this->player.scale_y - 1.0) < 0.05) this->player.scale_y = 1.01;
2023-03-17 16:38:10 +01:00
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;
2023-03-17 16:54:07 +01:00
this->player.scale_x = 0.75;
this->player.scale_y = 1.25;
2023-03-17 16:38:10 +01:00
break;
default:
case 0:
this->vel[1] = -3.8;
2023-03-17 16:54:07 +01:00
this->player.scale_x = 0.66;
this->player.scale_y = 1.33;
2023-03-17 16:38:10 +01:00
break;
case 1:
this->vel[1] = -4.8;
2023-03-17 16:54:07 +01:00
this->player.scale_x = 0.5;
this->player.scale_y = 1.5;
2023-03-17 16:38:10 +01:00
break;
}
2023-03-17 18:19:06 +01:00
} else if (on_ground && input_down(K_X)) {
this->vel[0] *= 3;
this->player.scale_x *= 1.05;
2023-03-17 16:38:10 +01:00
}
2023-03-17 11:06:37 +01:00
entity_move(this, g);
2023-03-17 19:03:59 +01:00
if (this->vel[0] == 0.0 && this->vel[1] >= -0.0)
2023-03-17 17:00:47 +01:00
this->player.dirx *= -1;
2023-03-17 21:07:07 +01:00
if (entity_place_meeting(this, g, ET_EXIT) != NULL)
g->queue_next_scene = true;
2023-03-17 10:51:17 +01:00
}
static void
player_draw(Entity *this, Game *g)
{
2023-03-17 10:57:19 +01:00
(void)g;
2023-03-17 13:45:20 +01:00
LZY_DrawSetColor(BLACK);
2023-03-17 16:54:07 +01:00
int width = (int)(this->width / 2) * this->player.scale_x;
int height = (int)(this->height / 2) * this->player.scale_y;
width *= 2;
height *= 2;
LZY_DrawRect(this->pos[0] - width / 2,
this->pos[1] - height / 2,
width, height);
2023-03-17 19:10:46 +01:00
//LZY_DrawRect(this->pos[0] - width / 2 + 1,
// this->pos[1] - height / 2 + 1,
// width - 2, height - 2);
2023-03-17 10:51:17 +01:00
}
void
2023-03-17 20:44:59 +01:00
player_init(Entity *this, int x, int y)
2023-03-17 10:51:17 +01:00
{
memset(this, 0, sizeof(*this));
2023-03-17 20:44:59 +01:00
this->pos[0] = x;
this->pos[1] = y;
2023-03-17 10:51:17 +01:00
this->type = ET_PLAYER;
this->update = player_update;
this->draw = player_draw;
2023-03-17 10:57:19 +01:00
this->width = 12;
this->height = 12;
2023-03-17 16:54:07 +01:00
this->player.scale_x = 1.0;
this->player.scale_y = 1.0;
2023-03-17 17:00:47 +01:00
this->player.dirx = 1;
2023-03-17 10:51:17 +01:00
}