2017-11-30 21:00:47 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include "player.h"
|
2017-12-15 08:08:45 +01:00
|
|
|
#include "monster.h"
|
2017-11-30 21:00:47 +01:00
|
|
|
|
2017-12-12 11:20:08 +01:00
|
|
|
static Stats classStats[] = {
|
2017-12-17 13:43:41 +01:00
|
|
|
(Stats) { 11, 5, 7, 2, 1, 1 }, /* ENGINEER */
|
|
|
|
(Stats) { 11, 5, 7, 2, 1, 1 }, /* MAGE */
|
|
|
|
(Stats) { 11, 5, 7, 2, 1, 1 }, /* PALADIN */
|
|
|
|
(Stats) { 11, 5, 7, 2, 2, 1 }, /* ROGUE */
|
|
|
|
(Stats) { 11, 5, 7, 2, 1, 1 }, /* WARRIOR */
|
2017-12-12 11:20:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
2017-12-15 08:08:45 +01:00
|
|
|
has_collided(Player *player, RoomMatrix *matrix)
|
2017-12-05 15:03:20 +01:00
|
|
|
{
|
2017-12-15 08:08:45 +01:00
|
|
|
bool collided = false;
|
|
|
|
|
|
|
|
Position roomCoord = position_to_room_coords(&player->sprite->pos);
|
2017-12-06 11:44:17 +01:00
|
|
|
if (roomCoord.x != matrix->roomPos.x || roomCoord.y != matrix->roomPos.y) {
|
2017-12-15 08:08:45 +01:00
|
|
|
return collided;
|
|
|
|
}
|
|
|
|
|
|
|
|
Position matrixPos = position_to_matrix_coords(&player->sprite->pos);
|
|
|
|
RoomSpace *space = &matrix->spaces[matrixPos.x][matrixPos.y];
|
|
|
|
collided = space->occupied;
|
|
|
|
|
|
|
|
if (space->monster != NULL) {
|
2017-12-15 15:03:29 +01:00
|
|
|
unsigned int hit = stats_fight(&player->stats,
|
|
|
|
&space->monster->stats);
|
2017-12-17 13:43:41 +01:00
|
|
|
if (hit > 0) {
|
2017-12-15 15:03:29 +01:00
|
|
|
space->monster->hitText->active = true;
|
2017-12-17 13:43:41 +01:00
|
|
|
space->monster->missText->active = false;
|
|
|
|
} else {
|
2017-12-15 15:03:29 +01:00
|
|
|
space->monster->missText->active = true;
|
2017-12-17 13:43:41 +01:00
|
|
|
space->monster->hitText->active = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (space->monster->stats.hp <= 0) {
|
|
|
|
// TODO(Linus): This needs some love later on.
|
|
|
|
player->xp += 10;
|
|
|
|
}
|
2017-12-06 11:44:17 +01:00
|
|
|
}
|
|
|
|
|
2017-12-15 08:08:45 +01:00
|
|
|
return collided;
|
2017-12-05 15:03:20 +01:00
|
|
|
}
|
|
|
|
|
2017-12-17 13:43:41 +01:00
|
|
|
static void
|
|
|
|
player_step(Player *p, RoomMatrix* m)
|
|
|
|
{
|
|
|
|
p->total_steps++;
|
|
|
|
p->steps++;
|
|
|
|
}
|
|
|
|
|
2017-12-12 11:20:08 +01:00
|
|
|
static void
|
2017-12-15 08:08:45 +01:00
|
|
|
move_left(Player *player, RoomMatrix *matrix)
|
2017-12-05 15:03:20 +01:00
|
|
|
{
|
2017-12-15 08:08:45 +01:00
|
|
|
player->sprite->clip.y = 16;
|
|
|
|
player->sprite->pos.x -= TILE_DIMENSION;
|
2017-12-17 13:43:41 +01:00
|
|
|
if (has_collided(player, matrix)) {
|
2017-12-15 08:08:45 +01:00
|
|
|
player->sprite->pos.x += TILE_DIMENSION;
|
2017-12-17 13:43:41 +01:00
|
|
|
}
|
|
|
|
player_step(player, matrix);
|
2017-12-05 15:03:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2017-12-15 08:08:45 +01:00
|
|
|
void move_right(Player *player, RoomMatrix *matrix)
|
2017-12-05 15:03:20 +01:00
|
|
|
{
|
2017-12-15 08:08:45 +01:00
|
|
|
player->sprite->clip.y = 32;
|
|
|
|
player->sprite->pos.x += TILE_DIMENSION;
|
2017-12-17 13:43:41 +01:00
|
|
|
if (has_collided(player, matrix)) {
|
2017-12-15 08:08:45 +01:00
|
|
|
player->sprite->pos.x -= TILE_DIMENSION;
|
2017-12-17 13:43:41 +01:00
|
|
|
}
|
|
|
|
player_step(player, matrix);
|
2017-12-05 15:03:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2017-12-15 08:08:45 +01:00
|
|
|
void move_up(Player *player, RoomMatrix *matrix)
|
2017-12-05 15:03:20 +01:00
|
|
|
{
|
2017-12-15 08:08:45 +01:00
|
|
|
player->sprite->clip.y = 48;
|
|
|
|
player->sprite->pos.y -= TILE_DIMENSION;
|
2017-12-17 13:43:41 +01:00
|
|
|
if (has_collided(player, matrix)) {
|
2017-12-15 08:08:45 +01:00
|
|
|
player->sprite->pos.y += TILE_DIMENSION;
|
2017-12-17 13:43:41 +01:00
|
|
|
}
|
|
|
|
player_step(player, matrix);
|
2017-12-05 15:03:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2017-12-15 08:08:45 +01:00
|
|
|
void move_down(Player *player, RoomMatrix *matrix)
|
2017-12-05 15:03:20 +01:00
|
|
|
{
|
2017-12-15 08:08:45 +01:00
|
|
|
player->sprite->clip.y = 0;
|
|
|
|
player->sprite->pos.y += TILE_DIMENSION;
|
2017-12-17 13:43:41 +01:00
|
|
|
if (has_collided(player, matrix)) {
|
2017-12-15 08:08:45 +01:00
|
|
|
player->sprite->pos.y -= TILE_DIMENSION;
|
2017-12-17 13:43:41 +01:00
|
|
|
}
|
|
|
|
player_step(player, matrix);
|
2017-12-05 15:03:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2017-12-15 08:08:45 +01:00
|
|
|
void handle_player_input(Player *player, RoomMatrix *matrix, SDL_Event *event)
|
2017-11-30 21:00:47 +01:00
|
|
|
{
|
|
|
|
static unsigned int step = 1;
|
|
|
|
|
|
|
|
if (event->type == SDL_KEYDOWN) {
|
|
|
|
switch (event->key.keysym.sym) {
|
2017-12-05 15:03:20 +01:00
|
|
|
case SDLK_LEFT:
|
2017-12-13 23:20:54 +01:00
|
|
|
case SDLK_h:
|
2017-12-15 08:08:45 +01:00
|
|
|
move_left(player, matrix);
|
2017-12-05 15:03:20 +01:00
|
|
|
break;
|
|
|
|
case SDLK_RIGHT:
|
2017-12-13 23:20:54 +01:00
|
|
|
case SDLK_l:
|
2017-12-15 08:08:45 +01:00
|
|
|
move_right(player, matrix);
|
2017-12-05 15:03:20 +01:00
|
|
|
break;
|
|
|
|
case SDLK_UP:
|
2017-12-13 23:20:54 +01:00
|
|
|
case SDLK_k:
|
2017-12-15 08:08:45 +01:00
|
|
|
move_up(player, matrix);
|
2017-12-05 15:03:20 +01:00
|
|
|
break;
|
|
|
|
case SDLK_DOWN:
|
2017-12-13 23:20:54 +01:00
|
|
|
case SDLK_j:
|
2017-12-15 08:08:45 +01:00
|
|
|
move_down(player, matrix);
|
2017-12-05 15:03:20 +01:00
|
|
|
break;
|
2017-11-30 21:00:47 +01:00
|
|
|
}
|
2017-12-15 08:08:45 +01:00
|
|
|
player->sprite->clip.x = 16*step;
|
2017-11-30 21:00:47 +01:00
|
|
|
if (step == 3)
|
|
|
|
step = 0;
|
|
|
|
else
|
|
|
|
++step;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-12 11:20:08 +01:00
|
|
|
Player*
|
|
|
|
player_create(class_t class, SDL_Renderer *renderer)
|
2017-11-30 21:00:47 +01:00
|
|
|
{
|
2017-12-12 11:20:08 +01:00
|
|
|
Player *player = malloc(sizeof(Player));
|
|
|
|
player->sprite = sprite_create();
|
2017-12-17 13:43:41 +01:00
|
|
|
player->total_steps = 0;
|
|
|
|
player->steps = 0;
|
|
|
|
player->xp = 0;
|
2017-11-30 21:00:47 +01:00
|
|
|
|
2017-12-12 11:20:08 +01:00
|
|
|
char asset[100];
|
2017-11-30 21:00:47 +01:00
|
|
|
switch (class) {
|
|
|
|
case ENGINEER:
|
|
|
|
strcpy(asset, "assets/Commissions/Engineer.png");
|
2017-12-12 11:20:08 +01:00
|
|
|
player->stats = classStats[ENGINEER];
|
2017-11-30 21:00:47 +01:00
|
|
|
break;
|
|
|
|
case MAGE:
|
|
|
|
strcpy(asset, "assets/Commissions/Mage.png");
|
2017-12-12 11:20:08 +01:00
|
|
|
player->stats = classStats[MAGE];
|
2017-11-30 21:00:47 +01:00
|
|
|
break;
|
|
|
|
case PALADIN:
|
|
|
|
strcpy(asset, "assets/Commissions/Paladin.png");
|
2017-12-12 11:20:08 +01:00
|
|
|
player->stats = classStats[PALADIN];
|
2017-11-30 21:00:47 +01:00
|
|
|
break;
|
|
|
|
case ROGUE:
|
|
|
|
strcpy(asset, "assets/Commissions/Rogue.png");
|
2017-12-12 11:20:08 +01:00
|
|
|
player->stats = classStats[ROGUE];
|
2017-11-30 21:00:47 +01:00
|
|
|
break;
|
|
|
|
case WARRIOR:
|
|
|
|
strcpy(asset, "assets/Commissions/Warrior.png");
|
2017-12-12 11:20:08 +01:00
|
|
|
player->stats = classStats[WARRIOR];
|
2017-11-30 21:00:47 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-12-12 11:20:08 +01:00
|
|
|
sprite_load_texture(player->sprite, asset, 0, renderer);
|
|
|
|
player->sprite->pos = (Position) { TILE_DIMENSION, TILE_DIMENSION };
|
2017-12-14 12:01:05 +01:00
|
|
|
player->sprite->clip = (SDL_Rect) { 0, 0, 16, 16 };
|
2017-12-12 11:20:08 +01:00
|
|
|
player->sprite->textures[0]->dim = (Dimension) {
|
|
|
|
TILE_DIMENSION, TILE_DIMENSION };
|
2017-12-15 08:08:45 +01:00
|
|
|
player->handle_event = &handle_player_input;
|
2017-11-30 21:00:47 +01:00
|
|
|
|
|
|
|
return player;
|
|
|
|
}
|
2017-12-12 11:20:08 +01:00
|
|
|
|
2017-12-17 13:43:41 +01:00
|
|
|
void
|
|
|
|
player_print(Player *p)
|
|
|
|
{
|
|
|
|
Position roomPos = position_to_matrix_coords(&p->sprite->pos);
|
|
|
|
Position pos = p->sprite->pos;
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
printf("--------=== <[ Player Stats ]> ===--------\n");
|
2017-12-18 09:59:01 +01:00
|
|
|
printf("HP: %d\n", p->stats.hp);
|
|
|
|
printf("Level: %d XP: %d\n", p->stats.lvl, p->xp);
|
2017-12-17 13:43:41 +01:00
|
|
|
printf("Pos: %dx%d RoomPos: %dx%d\n", pos.x, pos.y, roomPos.x, roomPos.y);
|
2017-12-18 09:59:01 +01:00
|
|
|
printf("Steps: %d\n", p->total_steps);
|
2017-12-17 13:43:41 +01:00
|
|
|
printf("------------------------------------------\n");
|
|
|
|
}
|
|
|
|
|
2017-12-12 11:20:08 +01:00
|
|
|
void
|
|
|
|
player_destroy(Player *player)
|
|
|
|
{
|
|
|
|
if (player->sprite)
|
|
|
|
sprite_destroy(player->sprite);
|
|
|
|
|
|
|
|
free(player);
|
|
|
|
}
|