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_Window *gWindow = NULL;
static SDL_Renderer *gRenderer = NULL; static SDL_Renderer *gRenderer = NULL;
static Sprite *gPlayer = NULL; static Player *gPlayer = NULL;
static LinkedList *gSpriteList = NULL; static LinkedList *gSpriteList = NULL;
static Map *gMap = NULL; static Map *gMap = NULL;
static RoomMatrix *gRoomMatrix = NULL; static RoomMatrix *gRoomMatrix = NULL;
@ -115,9 +115,11 @@ bool handle_events()
if (event.type == SDL_QUIT) { if (event.type == SDL_QUIT) {
quit = true; quit = true;
} else { } else {
gPlayer->handle_event(gPlayer, gRoomMatrix, &event); gPlayer->sprite->handle_event(gPlayer->sprite,
camera_follow_position(&gCamera, &gPlayer->pos); gRoomMatrix,
map_set_current_room(gMap, &gPlayer->pos); &event);
camera_follow_position(&gCamera, &gPlayer->sprite->pos);
map_set_current_room(gMap, &gPlayer->sprite->pos);
} }
} }
return quit; return quit;
@ -136,13 +138,13 @@ void run()
quit = handle_events(); quit = handle_events();
roommatrix_populate_from_map(gRoomMatrix, gMap); roommatrix_populate_from_map(gRoomMatrix, gMap);
roommatrix_add_lightsource(gRoomMatrix, roommatrix_add_lightsource(gRoomMatrix,
&gPlayer->pos); &gPlayer->sprite->pos);
roommatrix_build_lightmap(gRoomMatrix); roommatrix_build_lightmap(gRoomMatrix);
SDL_RenderClear(gRenderer); SDL_RenderClear(gRenderer);
map_render(gMap, &gCamera); map_render(gMap, &gCamera);
sprite_render(gPlayer, &gCamera); sprite_render(gPlayer->sprite, &gCamera);
roommatrix_render_lightmap(gRoomMatrix, &gCamera); roommatrix_render_lightmap(gRoomMatrix, &gCamera);
SDL_RenderPresent(gRenderer); SDL_RenderPresent(gRenderer);
@ -159,7 +161,7 @@ void run()
static static
void close() void close()
{ {
sprite_destroy(gPlayer); player_destroy(gPlayer);
map_destroy(gMap); map_destroy(gMap);
roommatrix_destroy(gRoomMatrix); roommatrix_destroy(gRoomMatrix);
SDL_DestroyWindow(gWindow); 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 <string.h>
#include "player.h" #include "player.h"
static static Stats classStats[] = {
bool has_collided(Sprite *sprite, RoomMatrix *matrix) (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); Position roomCoord = position_to_room_coords(&sprite->pos);
if (roomCoord.x != matrix->roomPos.x || roomCoord.y != matrix->roomPos.y) { 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; return matrix->spaces[matrixPos.x][matrixPos.y].occupied;
} }
static static void
void move_left(Sprite *sprite, RoomMatrix *matrix) move_left(Sprite *sprite, RoomMatrix *matrix)
{ {
sprite->textures[0]->clip.y = 16; sprite->textures[0]->clip.y = 16;
sprite->pos.x -= TILE_DIMENSION; 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(); Player *player = malloc(sizeof(Player));
char asset[100]; player->sprite = sprite_create();
char asset[100];
switch (class) { switch (class) {
case ENGINEER: case ENGINEER:
strcpy(asset, "assets/Commissions/Engineer.png"); strcpy(asset, "assets/Commissions/Engineer.png");
player->stats = classStats[ENGINEER];
break; break;
case MAGE: case MAGE:
strcpy(asset, "assets/Commissions/Mage.png"); strcpy(asset, "assets/Commissions/Mage.png");
player->stats = classStats[MAGE];
break; break;
case PALADIN: case PALADIN:
strcpy(asset, "assets/Commissions/Paladin.png"); strcpy(asset, "assets/Commissions/Paladin.png");
player->stats = classStats[PALADIN];
break; break;
case ROGUE: case ROGUE:
strcpy(asset, "assets/Commissions/Rogue.png"); strcpy(asset, "assets/Commissions/Rogue.png");
player->stats = classStats[ROGUE];
break; break;
case WARRIOR: case WARRIOR:
strcpy(asset, "assets/Commissions/Warrior.png"); strcpy(asset, "assets/Commissions/Warrior.png");
player->stats = classStats[WARRIOR];
break; break;
} }
sprite_load_texture(player, asset, 0, renderer); sprite_load_texture(player->sprite, asset, 0, renderer);
player->pos = (Position) { TILE_DIMENSION, TILE_DIMENSION }; player->sprite->pos = (Position) { TILE_DIMENSION, TILE_DIMENSION };
player->textures[0]->clip = (SDL_Rect) { 0, 0, 16, 16 }; player->sprite->textures[0]->clip = (SDL_Rect) { 0, 0, 16, 16 };
player->textures[0]->dim = (Dimension) { TILE_DIMENSION, TILE_DIMENSION }; player->sprite->textures[0]->dim = (Dimension) {
player->handle_event = &handle_player_input; TILE_DIMENSION, TILE_DIMENSION };
player->sprite->handle_event = &handle_player_input;
return player; 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 <SDL2/SDL.h>
#include "sprite.h" #include "sprite.h"
#include "stats.h"
enum PlayerClass { ENGINEER, MAGE, PALADIN, ROGUE, WARRIOR }; enum PlayerClass { ENGINEER, MAGE, PALADIN, ROGUE, WARRIOR };
typedef enum PlayerClass class_t; 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_ #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_