hyperultra/src/player.c

85 lines
2.1 KiB
C

#include "deathpart.h"
#include "entity.h"
#include "entityimpl.h"
#include "game.h"
#include "input.h"
#include <math.h>
IMPL_UPDATE() {
const int on_ground = entity_collide(this, g, 0, 1);
this->vel[0] = 2.0 * this->player.dirx;
this->vel[1] *= AIR_RESISTANCE;
this->vel[1] += GRAVITY;
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;
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;
this->player.scale_x = 0.75;
this->player.scale_y = 1.25;
break;
default:
case 0:
this->vel[1] = -3.8;
this->player.scale_x = 0.66;
this->player.scale_y = 1.33;
break;
case 1:
this->vel[1] = -4.8;
this->player.scale_x = 0.5;
this->player.scale_y = 1.5;
break;
}
} else if (on_ground && input_down(K_X)) {
this->vel[0] *= 3;
this->player.scale_x *= 1.05;
}
entity_move(this, g);
if (this->vel[0] == 0.0 && this->vel[1] >= -0.0)
this->player.dirx *= -1;
if (this->bonk_ceiling ||
entity_place_meeting(this, g, ET_spike) != NULL) {
int dy = this->pos[1] - 6;
for (int y = 0; y < 7; y++) {
int dx = this->pos[0] - 6;
for (int x = 0; x < 7; x++) {
deathpart_init(game_create_entity(g), dx, dy);
dx += 2;
}
dy += 2;
}
this->type = ET_NONE;
g->queue_restart_scene = 30;
}
if (entity_place_meeting(this, g, ET_exit) != NULL)
g->queue_next_scene = true;
} IMPL_END
IMPL_DRAW() {
LZY_DrawSetColor(BLACK);
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);
} IMPL_END
IMPL_INIT(player) {
this->width = 12;
this->height = 12;
this->player.scale_x = 1.0;
this->player.scale_y = 1.0;
this->player.dirx = 1;
} IMPL_END