Adds projectiles

Collisions still need implementing and an underlying skill + some drawing.
This commit is contained in:
Linus Probert 2018-03-08 00:58:26 +01:00
parent d564a559dc
commit be8677cb39
8 changed files with 153 additions and 0 deletions

View File

@ -116,6 +116,8 @@ add_executable(breakhack
src/skillbar
src/texturecache
src/skill
src/projectile
src/vector2d
)
target_link_libraries(breakhack

View File

@ -461,6 +461,7 @@ run_game(void)
gui_update_player_stats(gGui, gPlayer, gMap, gRenderer);
particle_engine_update(deltaTime);
player_update(gPlayer, deltaTime);
roommatrix_update_with_player(gRoomMatrix, gPlayer);
if (currentTurn == PLAYER) {

View File

@ -29,6 +29,8 @@
#include "keyboard.h"
#include "mixer.h"
#include "random.h"
#include "projectile.h"
#include "texturecache.h"
#define ENGINEER_STATS { 12, 12, 5, 7, 2, 2, 1 }
#define MAGE_STATS { 12, 12, 5, 7, 1, 2, 1 }
@ -332,6 +334,9 @@ handle_player_input(Player *player, RoomMatrix *matrix, SDL_Event *event)
if (event->type != SDL_KEYDOWN)
return;
if (player->projectiles)
return;
check_skill_activation(player, matrix, event);
if (!check_skill_trigger(player, matrix, event))
handle_movement_input(player, matrix, event);
@ -407,6 +412,7 @@ player_create(class_t class, SDL_Renderer *renderer)
player->sprite->dim = GAME_DIMENSION;
player->sprite->clip = (SDL_Rect) { 0, 0, 16, 16 };
player->handle_event = &handle_player_input;
player->projectiles = linkedlist_create();
player_load_texts(player, renderer);
return player;
@ -465,6 +471,12 @@ player_render(Player *player, Camera *cam)
sprite_render(player->sprite, cam);
actiontext_render(player->hitText, cam);
actiontext_render(player->missText, cam);
LinkedList *projectile = player->projectiles;
while (projectile) {
projectile_render(projectile->data, cam);
projectile = projectile->next;
}
}
static void
@ -490,6 +502,39 @@ player_reset_steps(Player *p)
player_print(p);
}
void player_update(Player *player, float deltatime)
{
if (!player->projectiles)
return;
LinkedList *last, *current, *next;
last = NULL;
current = player->projectiles;
next = NULL;
while (current) {
Projectile *p = current->data;
projectile_update(p, deltatime);
if (!p->alive) {
if (last == NULL)
player->projectiles = current->next;
else
last->next = current->next;
projectile_destroy(p);
next = current->next;
current->data = NULL;
current->next = NULL;
linkedlist_destroy(&current);
current = next;
} else {
last = current;
current = current->next;
}
}
}
void
player_destroy(Player *player)
{
@ -504,5 +549,8 @@ player_destroy(Player *player)
player->skills[i] = NULL;
}
while (player->projectiles)
projectile_destroy(linkedlist_pop(&player->projectiles));
free(player);
}

View File

@ -25,6 +25,7 @@
#include "actiontext.h"
#include "camera.h"
#include "skill.h"
#include "linkedlist.h"
#define PLAYER_SKILL_COUNT 5
@ -43,6 +44,7 @@ typedef struct Player_t {
ActionText *hitText;
ActionText *missText;
Stats stats;
LinkedList *projectiles;
unsigned int xp;
unsigned int total_steps;
unsigned int steps;
@ -74,6 +76,9 @@ player_hit(Player*, unsigned int dmg);
void
player_reset_steps(Player*);
void
player_update(Player*, float deltatime);
void
player_render(Player*, Camera*);

38
src/projectile.c Normal file
View File

@ -0,0 +1,38 @@
#include "projectile.h"
#include "util.h"
Projectile *
projectile_create(void)
{
Projectile *p = ec_malloc(sizeof(Projectile));
p->sprite = sprite_create();
p->velocity = VECTOR2D_NODIR;
p->alive = true;
p->lifetime = timer_create();
timer_start(p->lifetime);
return p;
}
void
projectile_update(Projectile *p, float deltatime)
{
p->sprite->pos.x += (int) (p->velocity.x * deltatime);
p->sprite->pos.y += (int) (p->velocity.y * deltatime);
if (timer_get_ticks(p->lifetime) > 2000)
p->alive = false;
}
void
projectile_render(Projectile *p, Camera *cam)
{
sprite_render(p->sprite, cam);
}
void
projectile_destroy(Projectile *p)
{
sprite_destroy(p->sprite);
timer_destroy(p->lifetime);
free(p);
}

47
src/projectile.h Normal file
View File

@ -0,0 +1,47 @@
/*
* BreakHack - A dungeone crawler RPG
* Copyright (C) 2018 Linus Probert <linus.probert@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PROJECTILE_H_
#define PROJECTILE_H_
#include <stdbool.h>
#include "sprite.h"
#include "camera.h"
#include "vector2d.h"
#include "timer.h"
typedef struct Prjectile_t {
Sprite *sprite;
Vector2d velocity;
Timer *lifetime;
bool alive;
} Projectile;
Projectile *
projectile_create(void);
void
projectile_update(Projectile*, float deltatime);
void
projectile_render(Projectile*, Camera*);
void
projectile_destroy(Projectile*);
#endif // PROJECTILE_H_

7
src/vector2d.c Normal file
View File

@ -0,0 +1,7 @@
#include "vector2d.h"
bool
vector2d_equals(Vector2d v1, Vector2d v2)
{
return v1.x == v2.x && v1.y == v2.y;
}

View File

@ -19,6 +19,8 @@
#ifndef VECTOR2D_H_
#define VECTOR2D_H_
#include <stdbool.h>
#define VECTOR2D_NODIR (Vector2d) { 0, 0 }
#define VECTOR2D_RIGHT (Vector2d) { 1, 0 }
#define VECTOR2D_LEFT (Vector2d) { -1, 0 }
@ -30,4 +32,7 @@ typedef struct Vector2d_t {
float y;
} Vector2d;
bool
vector2d_equals(Vector2d, Vector2d);
#endif // VECTOR2D_H_