More monster and combat prep.

I need a hashtable next.
This commit is contained in:
Linus Probert 2017-12-12 11:20:08 +01:00
parent 88bcc9c5ad
commit ac63f0b172
5 changed files with 79 additions and 20 deletions

View File

@ -15,7 +15,7 @@
static SDL_Window *gWindow = NULL;
static SDL_Renderer *gRenderer = NULL;
static Sprite *gPlayer = NULL;
static Player *gPlayer = NULL;
static LinkedList *gSpriteList = NULL;
static Map *gMap = NULL;
static RoomMatrix *gRoomMatrix = NULL;
@ -115,9 +115,11 @@ bool handle_events()
if (event.type == SDL_QUIT) {
quit = true;
} else {
gPlayer->handle_event(gPlayer, gRoomMatrix, &event);
camera_follow_position(&gCamera, &gPlayer->pos);
map_set_current_room(gMap, &gPlayer->pos);
gPlayer->sprite->handle_event(gPlayer->sprite,
gRoomMatrix,
&event);
camera_follow_position(&gCamera, &gPlayer->sprite->pos);
map_set_current_room(gMap, &gPlayer->sprite->pos);
}
}
return quit;
@ -136,13 +138,13 @@ void run()
quit = handle_events();
roommatrix_populate_from_map(gRoomMatrix, gMap);
roommatrix_add_lightsource(gRoomMatrix,
&gPlayer->pos);
&gPlayer->sprite->pos);
roommatrix_build_lightmap(gRoomMatrix);
SDL_RenderClear(gRenderer);
map_render(gMap, &gCamera);
sprite_render(gPlayer, &gCamera);
sprite_render(gPlayer->sprite, &gCamera);
roommatrix_render_lightmap(gRoomMatrix, &gCamera);
SDL_RenderPresent(gRenderer);
@ -159,7 +161,7 @@ void run()
static
void close()
{
sprite_destroy(gPlayer);
player_destroy(gPlayer);
map_destroy(gMap);
roommatrix_destroy(gRoomMatrix);
SDL_DestroyWindow(gWindow);

13
src/monster.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef MONSTER_H_
#define MONSTER_H_
#include <SDL2/SDL.h>
#include "sprite.h"
#include "stats.h"
typedef struct {
Sprite *sprite;
Stats stats;
} Monster;
#endif // MONSTER_H_

View File

@ -1,8 +1,16 @@
#include <string.h>
#include "player.h"
static
bool has_collided(Sprite *sprite, RoomMatrix *matrix)
static Stats classStats[] = {
(Stats) { 11, 11, 11, 1 }, /* ENGINEER */
(Stats) { 11, 11, 11, 1 }, /* MAGE */
(Stats) { 11, 11, 11, 1 }, /* PALADIN */
(Stats) { 11, 11, 11, 2 }, /* ROGUE */
(Stats) { 11, 11, 11, 1 }, /* WARRIOR */
};
static bool
has_collided(Sprite *sprite, RoomMatrix *matrix)
{
Position roomCoord = position_to_room_coords(&sprite->pos);
if (roomCoord.x != matrix->roomPos.x || roomCoord.y != matrix->roomPos.y) {
@ -13,8 +21,8 @@ bool has_collided(Sprite *sprite, RoomMatrix *matrix)
return matrix->spaces[matrixPos.x][matrixPos.y].occupied;
}
static
void move_left(Sprite *sprite, RoomMatrix *matrix)
static void
move_left(Sprite *sprite, RoomMatrix *matrix)
{
sprite->textures[0]->clip.y = 16;
sprite->pos.x -= TILE_DIMENSION;
@ -77,34 +85,51 @@ void handle_player_input(Sprite *sprite, RoomMatrix *matrix, SDL_Event *event)
}
}
Sprite* player_create(class_t class, SDL_Renderer *renderer)
Player*
player_create(class_t class, SDL_Renderer *renderer)
{
Sprite *player = sprite_create();
char asset[100];
Player *player = malloc(sizeof(Player));
player->sprite = sprite_create();
char asset[100];
switch (class) {
case ENGINEER:
strcpy(asset, "assets/Commissions/Engineer.png");
player->stats = classStats[ENGINEER];
break;
case MAGE:
strcpy(asset, "assets/Commissions/Mage.png");
player->stats = classStats[MAGE];
break;
case PALADIN:
strcpy(asset, "assets/Commissions/Paladin.png");
player->stats = classStats[PALADIN];
break;
case ROGUE:
strcpy(asset, "assets/Commissions/Rogue.png");
player->stats = classStats[ROGUE];
break;
case WARRIOR:
strcpy(asset, "assets/Commissions/Warrior.png");
player->stats = classStats[WARRIOR];
break;
}
sprite_load_texture(player, asset, 0, renderer);
player->pos = (Position) { TILE_DIMENSION, TILE_DIMENSION };
player->textures[0]->clip = (SDL_Rect) { 0, 0, 16, 16 };
player->textures[0]->dim = (Dimension) { TILE_DIMENSION, TILE_DIMENSION };
player->handle_event = &handle_player_input;
sprite_load_texture(player->sprite, asset, 0, renderer);
player->sprite->pos = (Position) { TILE_DIMENSION, TILE_DIMENSION };
player->sprite->textures[0]->clip = (SDL_Rect) { 0, 0, 16, 16 };
player->sprite->textures[0]->dim = (Dimension) {
TILE_DIMENSION, TILE_DIMENSION };
player->sprite->handle_event = &handle_player_input;
return player;
}
void
player_destroy(Player *player)
{
if (player->sprite)
sprite_destroy(player->sprite);
free(player);
}

View File

@ -3,10 +3,18 @@
#include <SDL2/SDL.h>
#include "sprite.h"
#include "stats.h"
enum PlayerClass { ENGINEER, MAGE, PALADIN, ROGUE, WARRIOR };
typedef enum PlayerClass class_t;
Sprite* player_create(class_t, SDL_Renderer*);
typedef struct {
Sprite *sprite;
Stats stats;
} Player;
Player* player_create(class_t, SDL_Renderer*);
void player_destroy(Player*);
#endif // PLAYER_H_

11
src/stats.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef STATS_H_
#define STATS_H_
typedef struct {
unsigned int hp; /* Hit points */
unsigned int dmg; /* Damage modifier */
unsigned int atk; /* Attack rating */
unsigned int speed; /* Speed */
} Stats;
#endif // STATS_H_