Hit and miss information.
This commit is contained in:
parent
916193ecb3
commit
7389c2d588
|
@ -45,6 +45,7 @@ add_executable(breakhack
|
||||||
src/position
|
src/position
|
||||||
src/monster
|
src/monster
|
||||||
src/stats
|
src/stats
|
||||||
|
src/actiontext
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(breakhack
|
target_link_libraries(breakhack
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
#include "actiontext.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
ActionText*
|
||||||
|
actiontext_create()
|
||||||
|
{
|
||||||
|
ActionText *t = ec_malloc(sizeof(ActionText));
|
||||||
|
t->pos = (Position) { 0, 0 };
|
||||||
|
t->texture = texture_create();
|
||||||
|
t->timer = timer_create();
|
||||||
|
t->active = false;
|
||||||
|
t->color = (SDL_Color) { 255, 255, 255 };
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
actiontext_load_font(ActionText *t, const char *path, unsigned int size)
|
||||||
|
{
|
||||||
|
texture_load_font(t->texture, path, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
actiontext_set_text(ActionText *t, const char *text, SDL_Renderer *renderer)
|
||||||
|
{
|
||||||
|
texture_load_from_text(t->texture, text, t->color, renderer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
actiontext_render(ActionText *t, Camera *cam)
|
||||||
|
{
|
||||||
|
if (!t->active)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (t->active && !timer_started(t->timer))
|
||||||
|
timer_start(t->timer);
|
||||||
|
|
||||||
|
Position cameraPos = camera_to_camera_position(cam, &t->pos);
|
||||||
|
if (timer_get_ticks(t->timer) < 100) {
|
||||||
|
texture_render(t->texture, &cameraPos, cam);
|
||||||
|
} else {
|
||||||
|
timer_stop(t->timer);
|
||||||
|
t->active = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
actiontext_destroy(ActionText *t)
|
||||||
|
{
|
||||||
|
texture_destroy(t->texture);
|
||||||
|
timer_destroy(t->timer);
|
||||||
|
free(t);
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
#ifndef ACTIONTEXT_H_
|
||||||
|
#define ACTIONTEXT_H_
|
||||||
|
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
|
||||||
|
#include "position.h"
|
||||||
|
#include "texture.h"
|
||||||
|
#include "timer.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
Position pos;
|
||||||
|
Texture *texture;
|
||||||
|
bool active;
|
||||||
|
Timer *timer;
|
||||||
|
SDL_Color color;
|
||||||
|
} ActionText;
|
||||||
|
|
||||||
|
ActionText* actiontext_create();
|
||||||
|
|
||||||
|
void actiontext_load_font(ActionText*, const char *path, unsigned int size);
|
||||||
|
|
||||||
|
void actiontext_set_text(ActionText*, const char *text, SDL_Renderer*);
|
||||||
|
|
||||||
|
void actiontext_render(ActionText*, Camera*);
|
||||||
|
|
||||||
|
void actiontext_destroy(ActionText*);
|
||||||
|
|
||||||
|
#endif // ACTIONTEXT_H_
|
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef GLOBALS_H_
|
||||||
|
#define GLOBALS_H_
|
||||||
|
|
||||||
|
#include <SDL2/SDL_ttf.h>
|
||||||
|
|
||||||
|
TTF_Font *gFontLarge = NULL;
|
||||||
|
TTF_Font *gFontSmall = NULL;
|
||||||
|
|
||||||
|
#endif // GLOBALS_H_
|
12
src/main.c
12
src/main.c
|
@ -65,12 +65,20 @@ bool initSDL()
|
||||||
}
|
}
|
||||||
if (SDL_RenderSetLogicalSize(gRenderer, SCREEN_WIDTH, SCREEN_HEIGHT) < 0)
|
if (SDL_RenderSetLogicalSize(gRenderer, SCREEN_WIDTH, SCREEN_HEIGHT) < 0)
|
||||||
{
|
{
|
||||||
printf("[!!] Unable to initiate scaling: %s\n", SDL_GetError());
|
printf("[!!] Unable to initiate scaling: %s\n",
|
||||||
|
SDL_GetError());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( (IMG_Init(imgFlags) & imgFlags) == 0 ) {
|
if ( (IMG_Init(imgFlags) & imgFlags) == 0 ) {
|
||||||
printf("[!!] Unable to initiate img loading: %s\n", IMG_GetError());
|
printf("[!!] Unable to initiate img loading: %s\n",
|
||||||
|
IMG_GetError());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( TTF_Init() == -1 ) {
|
||||||
|
printf("[!!] Unable to initiate ttf library: %s\n",
|
||||||
|
TTF_GetError());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -68,7 +68,8 @@ map_add_monster_texture(Map *map, const char *path, SDL_Renderer *renderer)
|
||||||
|
|
||||||
t = ht_get(map->monsterTextures, path);
|
t = ht_get(map->monsterTextures, path);
|
||||||
if (!t) {
|
if (!t) {
|
||||||
t = texture_create(path, renderer);
|
t = texture_create();
|
||||||
|
texture_load_from_file(t, path, renderer);
|
||||||
ht_set(map->monsterTextures, path, t);
|
ht_set(map->monsterTextures, path, t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,7 +112,8 @@ map_add_monster(Map *map, Monster *m)
|
||||||
|
|
||||||
int map_add_texture(Map *map, const char *path, SDL_Renderer *renderer)
|
int map_add_texture(Map *map, const char *path, SDL_Renderer *renderer)
|
||||||
{
|
{
|
||||||
Texture *t = texture_create(path, renderer);
|
Texture *t = texture_create();
|
||||||
|
texture_load_from_file(t, path, renderer);
|
||||||
linkedlist_append(&map->textures, t);
|
linkedlist_append(&map->textures, t);
|
||||||
return linkedlist_size(map->textures) - 1;
|
return linkedlist_size(map->textures) - 1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -181,9 +181,9 @@ l_add_monster(lua_State *L)
|
||||||
|
|
||||||
lua_pop(L, 4);
|
lua_pop(L, 4);
|
||||||
|
|
||||||
monster = monster_create();
|
monster = monster_create(renderer);
|
||||||
monster->sprite->clip = (SDL_Rect) { clip_x, clip_y, 16, 16 };
|
monster->sprite->clip = (SDL_Rect) { clip_x, clip_y, 16, 16 };
|
||||||
monster->sprite->pos = (Position) { x, y };
|
monster_update_pos(monster, (Position) { x, y });
|
||||||
sprite_set_texture(monster->sprite, texture1, 0);
|
sprite_set_texture(monster->sprite, texture1, 0);
|
||||||
sprite_set_texture(monster->sprite, texture2, 1);
|
sprite_set_texture(monster->sprite, texture2, 1);
|
||||||
|
|
||||||
|
|
|
@ -3,25 +3,65 @@
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
#include "monster.h"
|
#include "monster.h"
|
||||||
|
|
||||||
|
static void
|
||||||
|
monster_load_texts(Monster *m, SDL_Renderer *renderer)
|
||||||
|
{
|
||||||
|
ActionText *t = actiontext_create();
|
||||||
|
actiontext_load_font(t, "assets/GUI/SDS_6x6.ttf", 14);
|
||||||
|
t->color = (SDL_Color) { 255, 100, 0 };
|
||||||
|
actiontext_set_text(t, "HIT", renderer);
|
||||||
|
t->pos = m->sprite->pos;
|
||||||
|
m->hitText = t;
|
||||||
|
|
||||||
|
t = actiontext_create();
|
||||||
|
actiontext_load_font(t, "assets/GUI/SDS_6x6.ttf", 14);
|
||||||
|
t->color = (SDL_Color) { 255, 255, 0 };
|
||||||
|
actiontext_set_text(t, "MISS", renderer);
|
||||||
|
t->pos = m->sprite->pos;
|
||||||
|
m->missText = t;
|
||||||
|
}
|
||||||
|
|
||||||
Monster*
|
Monster*
|
||||||
monster_create()
|
monster_create(SDL_Renderer *renderer)
|
||||||
{
|
{
|
||||||
Monster *m = ec_malloc(sizeof(Monster));
|
Monster *m = ec_malloc(sizeof(Monster));
|
||||||
m->sprite = sprite_create();
|
m->sprite = sprite_create();
|
||||||
m->sprite->clip = (SDL_Rect) { 0, 0, 16, 16 };
|
m->sprite->clip = (SDL_Rect) { 0, 0, 16, 16 };
|
||||||
m->stats = (Stats) { 11, 1, 0, 0, 1 };
|
m->stats = (Stats) { 11, 1, 0, 0, 1 };
|
||||||
|
|
||||||
|
monster_load_texts(m, renderer);
|
||||||
|
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
monster_update_pos(Monster *m, Position pos)
|
||||||
|
{
|
||||||
|
m->sprite->pos = pos;
|
||||||
|
|
||||||
|
Position textPos = pos;
|
||||||
|
textPos.y += 10;
|
||||||
|
m->hitText->pos = textPos;
|
||||||
|
m->missText->pos = textPos;
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
monster_render(Monster *m, Camera *cam)
|
monster_render(Monster *m, Camera *cam)
|
||||||
{
|
{
|
||||||
sprite_render(m->sprite, cam);
|
sprite_render(m->sprite, cam);
|
||||||
|
if (m->hitText)
|
||||||
|
actiontext_render(m->hitText, cam);
|
||||||
|
if (m->missText)
|
||||||
|
actiontext_render(m->missText, cam);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
monster_destroy(Monster *m)
|
monster_destroy(Monster *m)
|
||||||
{
|
{
|
||||||
sprite_destroy(m->sprite);
|
sprite_destroy(m->sprite);
|
||||||
|
if (m->hitText)
|
||||||
|
actiontext_destroy(m->hitText);
|
||||||
|
if (m->missText)
|
||||||
|
actiontext_destroy(m->missText);
|
||||||
free(m);
|
free(m);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,13 +4,18 @@
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
#include "sprite.h"
|
#include "sprite.h"
|
||||||
#include "stats.h"
|
#include "stats.h"
|
||||||
|
#include "actiontext.h"
|
||||||
|
|
||||||
typedef struct Monster_t {
|
typedef struct Monster_t {
|
||||||
Sprite *sprite;
|
Sprite *sprite;
|
||||||
|
ActionText *hitText;
|
||||||
|
ActionText *missText;
|
||||||
Stats stats;
|
Stats stats;
|
||||||
} Monster;
|
} Monster;
|
||||||
|
|
||||||
Monster* monster_create();
|
Monster* monster_create(SDL_Renderer*);
|
||||||
|
|
||||||
|
void monster_update_pos(Monster*, Position);
|
||||||
|
|
||||||
void monster_render(Monster*, Camera*);
|
void monster_render(Monster*, Camera*);
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,12 @@ has_collided(Player *player, RoomMatrix *matrix)
|
||||||
collided = space->occupied;
|
collided = space->occupied;
|
||||||
|
|
||||||
if (space->monster != NULL) {
|
if (space->monster != NULL) {
|
||||||
stats_fight(&player->stats, &space->monster->stats);
|
unsigned int hit = stats_fight(&player->stats,
|
||||||
|
&space->monster->stats);
|
||||||
|
if (hit > 0)
|
||||||
|
space->monster->hitText->active = true;
|
||||||
|
else
|
||||||
|
space->monster->missText->active = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return collided;
|
return collided;
|
||||||
|
|
|
@ -39,7 +39,8 @@ sprite_load_texture(Sprite *sprite,
|
||||||
sprite->textures[index] = NULL;
|
sprite->textures[index] = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
sprite->textures[index] = texture_create(path, renderer);
|
sprite->textures[index] = texture_create();
|
||||||
|
texture_load_from_file(sprite->textures[index], path, renderer);
|
||||||
sprite->destroyTextures = true;
|
sprite->destroyTextures = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
11
src/stats.c
11
src/stats.c
|
@ -2,9 +2,11 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <SDL2/SDL_ttf.h>
|
||||||
|
|
||||||
#include "stats.h"
|
#include "stats.h"
|
||||||
|
|
||||||
void
|
unsigned int
|
||||||
stats_fight(Stats *attacker, Stats *defender)
|
stats_fight(Stats *attacker, Stats *defender)
|
||||||
{
|
{
|
||||||
unsigned int atkRoll, defRoll, dmgRoll;
|
unsigned int atkRoll, defRoll, dmgRoll;
|
||||||
|
@ -16,13 +18,16 @@ stats_fight(Stats *attacker, Stats *defender)
|
||||||
critical = true;
|
critical = true;
|
||||||
atkRoll += attacker->atk;
|
atkRoll += attacker->atk;
|
||||||
defRoll = (rand() % 20) + defender->def;
|
defRoll = (rand() % 20) + defender->def;
|
||||||
|
dmgRoll = 0;
|
||||||
|
|
||||||
printf("Attacking: %d, Defending: %d\n", atkRoll, defRoll);
|
//printf("Attacking: %u, Defending: %u\n", atkRoll, defRoll);
|
||||||
if (atkRoll > defRoll) {
|
if (atkRoll > defRoll) {
|
||||||
dmgRoll = (rand() % 8) + attacker->dmg;
|
dmgRoll = (rand() % 8) + attacker->dmg;
|
||||||
defender->hp -= dmgRoll;
|
defender->hp -= dmgRoll;
|
||||||
if (critical)
|
if (critical)
|
||||||
defender->hp -= dmgRoll;
|
defender->hp -= dmgRoll;
|
||||||
}
|
}
|
||||||
printf("Attacker hp: %d, Defender hp: %d\n", attacker->hp, defender->hp);
|
//printf("Attacker hp: %u, Defender hp: %u\n", attacker->hp, defender->hp);
|
||||||
|
|
||||||
|
return dmgRoll;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ typedef struct {
|
||||||
int speed; /* Speed */
|
int speed; /* Speed */
|
||||||
} Stats;
|
} Stats;
|
||||||
|
|
||||||
void
|
unsigned int
|
||||||
stats_fight(Stats *attacker, Stats *defender);
|
stats_fight(Stats *attacker, Stats *defender);
|
||||||
|
|
||||||
#endif // STATS_H_
|
#endif // STATS_H_
|
||||||
|
|
|
@ -2,23 +2,41 @@
|
||||||
#include "texture.h"
|
#include "texture.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
Texture* texture_create(const char *path, SDL_Renderer *renderer)
|
Texture*
|
||||||
|
texture_create()
|
||||||
|
{
|
||||||
|
Texture *t = ec_malloc(sizeof(Texture));
|
||||||
|
t->dim.height = 0;
|
||||||
|
t->dim.width = 0;
|
||||||
|
t->texture = NULL;
|
||||||
|
t->font = NULL;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
texture_load_from_file(Texture *texture,
|
||||||
|
const char *path,
|
||||||
|
SDL_Renderer *renderer)
|
||||||
{
|
{
|
||||||
Texture *newTexture = NULL;
|
|
||||||
SDL_Surface *surface = IMG_Load(path);
|
SDL_Surface *surface = IMG_Load(path);
|
||||||
|
|
||||||
if (surface == NULL)
|
if (surface == NULL)
|
||||||
{
|
{
|
||||||
printf("[!!] Failed to load texture (%s): %s\n", path, IMG_GetError());
|
printf("[!!] Failed to load texture (%s): %s\n",
|
||||||
|
path, IMG_GetError());
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
if (texture->texture) {
|
||||||
newTexture = ec_malloc(sizeof(Texture));
|
SDL_DestroyTexture(texture->texture);
|
||||||
newTexture->dim.height = surface->h;
|
texture->texture = NULL;
|
||||||
newTexture->dim.width = surface->w;
|
}
|
||||||
newTexture->texture = SDL_CreateTextureFromSurface(renderer,
|
|
||||||
|
texture->dim.height = surface->h;
|
||||||
|
texture->dim.width = surface->w;
|
||||||
|
texture->texture = SDL_CreateTextureFromSurface(renderer,
|
||||||
surface);
|
surface);
|
||||||
if (newTexture->texture == NULL)
|
if (texture->texture == NULL)
|
||||||
{
|
{
|
||||||
printf("[!!] Failed to create texture (%s): %s\n",
|
printf("[!!] Failed to create texture (%s): %s\n",
|
||||||
path,
|
path,
|
||||||
|
@ -28,12 +46,58 @@ Texture* texture_create(const char *path, SDL_Renderer *renderer)
|
||||||
SDL_FreeSurface(surface);
|
SDL_FreeSurface(surface);
|
||||||
}
|
}
|
||||||
|
|
||||||
return newTexture;
|
void
|
||||||
|
texture_load_font(Texture *t, const char *path, unsigned int size)
|
||||||
|
{
|
||||||
|
if (t->font)
|
||||||
|
TTF_CloseFont(t->font);
|
||||||
|
t->font = TTF_OpenFont(path, size);
|
||||||
|
if (t->font == NULL) {
|
||||||
|
fprintf(stderr, "[!!] Failed to load font %s: %s\n",
|
||||||
|
path,
|
||||||
|
TTF_GetError());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
texture_load_from_text(Texture *t,
|
||||||
|
const char *text,
|
||||||
|
SDL_Color c,
|
||||||
|
SDL_Renderer *renderer)
|
||||||
|
{
|
||||||
|
SDL_Surface *surface = TTF_RenderText_Solid( t->font, text, c );
|
||||||
|
if (surface == NULL)
|
||||||
|
{
|
||||||
|
printf("[!!] Unable to create texture from rendered text: %s\n",
|
||||||
|
IMG_GetError());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (t->texture) {
|
||||||
|
SDL_DestroyTexture(t->texture);
|
||||||
|
t->texture = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
t->texture = SDL_CreateTextureFromSurface( renderer, surface );
|
||||||
|
if (t->texture == NULL) {
|
||||||
|
printf("[!!] Failed to create texture from text: %s\n",
|
||||||
|
SDL_GetError());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
t->dim.width = surface->w;
|
||||||
|
t->dim.height = surface->h;
|
||||||
|
|
||||||
|
SDL_FreeSurface(surface);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
texture_render(Texture *texture, Position *p, Camera *cam)
|
texture_render(Texture *texture, Position *p, Camera *cam)
|
||||||
{
|
{
|
||||||
|
if (!texture->texture)
|
||||||
|
return;
|
||||||
|
|
||||||
SDL_Rect draw_box = (SDL_Rect) {
|
SDL_Rect draw_box = (SDL_Rect) {
|
||||||
p->x,
|
p->x,
|
||||||
p->y,
|
p->y,
|
||||||
|
@ -72,6 +136,9 @@ texture_render_clip(Texture *texture, Position *p, SDL_Rect *clip, Camera *cam)
|
||||||
|
|
||||||
void texture_destroy(Texture *texture)
|
void texture_destroy(Texture *texture)
|
||||||
{
|
{
|
||||||
|
if (texture->texture)
|
||||||
SDL_DestroyTexture(texture->texture);
|
SDL_DestroyTexture(texture->texture);
|
||||||
|
if (texture->font)
|
||||||
|
TTF_CloseFont(texture->font);
|
||||||
free(texture);
|
free(texture);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,16 +2,27 @@
|
||||||
#define TEXTURE_H_
|
#define TEXTURE_H_
|
||||||
|
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
#include <SDL2/SDL_ttf.h>
|
||||||
#include "dimension.h"
|
#include "dimension.h"
|
||||||
#include "position.h"
|
#include "position.h"
|
||||||
#include "camera.h"
|
#include "camera.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
SDL_Texture *texture;
|
SDL_Texture *texture;
|
||||||
|
TTF_Font *font;
|
||||||
Dimension dim;
|
Dimension dim;
|
||||||
} Texture;
|
} Texture;
|
||||||
|
|
||||||
Texture* texture_create(const char *path, SDL_Renderer *renderer);
|
Texture* texture_create();
|
||||||
|
|
||||||
|
void texture_load_from_file(Texture*, const char *path, SDL_Renderer*);
|
||||||
|
|
||||||
|
void texture_load_font(Texture*, const char *path, unsigned int size);
|
||||||
|
|
||||||
|
void texture_load_from_text(Texture*,
|
||||||
|
const char *text,
|
||||||
|
SDL_Color,
|
||||||
|
SDL_Renderer*);
|
||||||
|
|
||||||
void texture_render(Texture*, Position*, Camera*);
|
void texture_render(Texture*, Position*, Camera*);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue