diff --git a/CMakeLists.txt b/CMakeLists.txt index e4dab5d..08440e5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -116,6 +116,8 @@ add_executable(breakhack src/skillbar src/texturecache src/skill + src/projectile + src/vector2d ) target_link_libraries(breakhack diff --git a/src/main.c b/src/main.c index 4090ca8..ab3a7b8 100644 --- a/src/main.c +++ b/src/main.c @@ -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) { diff --git a/src/player.c b/src/player.c index 09b20a2..5e1238c 100644 --- a/src/player.c +++ b/src/player.c @@ -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(¤t); + 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); } diff --git a/src/player.h b/src/player.h index 051d8c8..8049fa6 100644 --- a/src/player.h +++ b/src/player.h @@ -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*); diff --git a/src/projectile.c b/src/projectile.c new file mode 100644 index 0000000..9ddfa1d --- /dev/null +++ b/src/projectile.c @@ -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); +} \ No newline at end of file diff --git a/src/projectile.h b/src/projectile.h new file mode 100644 index 0000000..c78b00b --- /dev/null +++ b/src/projectile.h @@ -0,0 +1,47 @@ +/* + * BreakHack - A dungeone crawler RPG + * Copyright (C) 2018 Linus Probert + * + * 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 . + */ + +#ifndef PROJECTILE_H_ +#define PROJECTILE_H_ + +#include +#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_ \ No newline at end of file diff --git a/src/vector2d.c b/src/vector2d.c new file mode 100644 index 0000000..5eb5cf5 --- /dev/null +++ b/src/vector2d.c @@ -0,0 +1,7 @@ +#include "vector2d.h" + +bool +vector2d_equals(Vector2d v1, Vector2d v2) +{ + return v1.x == v2.x && v1.y == v2.y; +} \ No newline at end of file diff --git a/src/vector2d.h b/src/vector2d.h index 92777d3..20f9fdb 100644 --- a/src/vector2d.h +++ b/src/vector2d.h @@ -19,6 +19,8 @@ #ifndef VECTOR2D_H_ #define VECTOR2D_H_ +#include + #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_