From 9540aaf0d54bb40de2e450df5e78e811f0e27646 Mon Sep 17 00:00:00 2001
From: Anonymus Raccoon
Date: Tue, 28 Apr 2020 16:04:13 +0200
Subject: [PATCH] Adding attacks to the combat system
---
include/player_utilities.h | 3 +-
include/setup.h | 1 +
include/systems/combat_manager.h | 2 +
lib/gamacon | 2 +-
prefabs/combat.gcprefab | 40 ++++++++++-------
prefabs/enemies/bee.gcprefab | 2 +-
src/combat/attacks.c | 77 +++++++++++++++++++++++++++++---
src/game_loader.c | 3 +-
src/player_utilities.c | 21 +++++----
src/systems/combat_manager.c | 2 +-
src/systems/combat_methods.c | 9 +++-
11 files changed, 125 insertions(+), 37 deletions(-)
diff --git a/include/player_utilities.h b/include/player_utilities.h
index fe13434..5b1a55d 100644
--- a/include/player_utilities.h
+++ b/include/player_utilities.h
@@ -10,6 +10,7 @@
#include "entity.h"
-void set_combat_player(gc_entity *main_player, gc_entity *combat_player);
+void set_combat_player(gc_engine *engine, gc_entity *main_player, \
+gc_entity *combat_player);
#endif //MY_RPG_PLAYER_UTILITIES_H
diff --git a/include/setup.h b/include/setup.h
index 329a7bf..c1d9675 100644
--- a/include/setup.h
+++ b/include/setup.h
@@ -59,5 +59,6 @@ void fireball(gc_engine *engine, gc_entity *from, gc_entity *enemy);
void uppercut(gc_engine *engine, gc_entity *from, gc_entity *enemy);
void water_jet(gc_engine *engine, gc_entity *from, gc_entity *enemy);
void shield(gc_engine *engine, gc_entity *from, gc_entity *enemy);
+void ennemie_attack(gc_engine *engine, gc_entity *from, gc_entity *enemy);
void load_attacks(gc_scene *scene);
\ No newline at end of file
diff --git a/include/systems/combat_manager.h b/include/systems/combat_manager.h
index e90af2b..b2158ba 100644
--- a/include/systems/combat_manager.h
+++ b/include/systems/combat_manager.h
@@ -29,6 +29,8 @@ struct combat_manager {
struct enemy *current_enemy;
struct attack_holder *next_enemy_attack;
enum combat_state state;
+ char *last_attack;
+ int last_damage;
};
extern const struct combat_manager combat_manager;
diff --git a/lib/gamacon b/lib/gamacon
index c67bb8b..8493449 160000
--- a/lib/gamacon
+++ b/lib/gamacon
@@ -1 +1 @@
-Subproject commit c67bb8b37d159e52eb88e156e6da20efabbec586
+Subproject commit 8493449a020472127da0468bf1159c33aab0d5b7
diff --git a/prefabs/combat.gcprefab b/prefabs/combat.gcprefab
index a1a0e8f..426f214 100644
--- a/prefabs/combat.gcprefab
+++ b/prefabs/combat.gcprefab
@@ -7,6 +7,7 @@
+
@@ -25,22 +26,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -52,5 +37,28 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/prefabs/enemies/bee.gcprefab b/prefabs/enemies/bee.gcprefab
index 54af282..1859c27 100644
--- a/prefabs/enemies/bee.gcprefab
+++ b/prefabs/enemies/bee.gcprefab
@@ -10,7 +10,7 @@
-
+
diff --git a/src/combat/attacks.c b/src/combat/attacks.c
index 77bae18..1633672 100644
--- a/src/combat/attacks.c
+++ b/src/combat/attacks.c
@@ -5,27 +5,94 @@
** fireball.c
*/
-#include
+#include
+#include
+#include
+#include
+#include "components/health_component.h"
#include "engine.h"
+#include "renderer.h"
void uppercut(gc_engine *engine, gc_entity *from, gc_entity *enemy)
{
+ struct combat_manager *this = GETSYS(engine, combat_manager);
+ struct game_manager_system *inv = GETSYS(engine, game_manager_system);
struct health_component *enemy_health = GETCMP(enemy, health_component);
+ struct renderer *rend = GETCMP(from, renderer);
+ int max;
- rem_health(enemy_health, engine, 10);
+ if (this->last_attack && my_strcmp(this->last_attack, "uppercut"))
+ max = 20 - this->last_damage;
+ else
+ max = 10;
+ this->last_damage = MAX(random() % max, 10);
+ this->last_attack = "uppercut";
+ rem_health(enemy_health, engine, this->last_damage);
+ if (rend)
+ rend_set_anim(rend, "uppercut");
}
void water_jet(gc_engine *engine, gc_entity *from, gc_entity *enemy)
{
- printf("water_grenade\n");
+ struct combat_manager *this = GETSYS(engine, combat_manager);
+ struct game_manager_system *inv = GETSYS(engine, game_manager_system);
+ struct health_component *enemy_health = GETCMP(enemy, health_component);
+ struct renderer *rend = GETCMP(from, renderer);
+ int max;
+
+ if (this->last_attack && my_strcmp(this->last_attack, "water_jet"))
+ max = 20 - this->last_damage;
+ else
+ max = 10;
+ this->last_damage = MAX(random() % max, 10);
+ this->last_attack = "water_jet";
+ rem_health(enemy_health, engine, this->last_damage);
+ if (rend)
+ rend_set_anim(rend, "water_jet");
}
void fireball(gc_engine *engine, gc_entity *from, gc_entity *enemy)
{
- printf("FIREBALL\n");
+ struct combat_manager *this = GETSYS(engine, combat_manager);
+ struct game_manager_system *inv = GETSYS(engine, game_manager_system);
+ struct health_component *enemy_health = GETCMP(enemy, health_component);
+ struct renderer *rend = GETCMP(from, renderer);
+ int max;
+
+ if (this->last_attack && my_strcmp(this->last_attack, "fireball"))
+ max = 20 - this->last_damage;
+ else
+ max = 10;
+ this->last_damage = MAX(random() % max, 10);
+ this->last_attack = "fireball";
+ rem_health(enemy_health, engine, this->last_damage);
+ if (rend)
+ rend_set_anim(rend, "fireball");
}
void shield(gc_engine *engine, gc_entity *from, gc_entity *enemy)
{
- printf("shield\n");
+ struct combat_manager *this = GETSYS(engine, combat_manager);
+ struct game_manager_system *inv = GETSYS(engine, game_manager_system);
+ struct health_component *enemy_health = GETCMP(enemy, health_component);
+ struct renderer *rend = GETCMP(from, renderer);
+
+ this->last_damage = 50;
+ this->last_attack = "shield";
+ if (rend)
+ rend_set_anim(rend, "shield");
+}
+
+void ennemie_attack(gc_engine *engine, gc_entity *from, gc_entity *enemy)
+{
+ struct combat_manager *this = GETSYS(engine, combat_manager);
+ struct health_component *enemy_health = GETCMP(enemy, health_component);
+ struct renderer *rend = GETCMP(from, renderer);
+ float amount = random() % 10;
+
+ if (this->last_attack && !my_strcmp(this->last_attack, "shield"))
+ amount /= (float)this->last_damage / 100;
+ rem_health(enemy_health, engine, (int)amount);
+ if (rend)
+ rend_set_anim(rend, "attack");
}
diff --git a/src/game_loader.c b/src/game_loader.c
index 86d7794..2e4970d 100644
--- a/src/game_loader.c
+++ b/src/game_loader.c
@@ -64,8 +64,9 @@ const struct callback map_editor_callbacks[] = {
const struct gc_data attacks[] = {
{"attack", "Uppercut", &uppercut, NULL},
{"attack", "Fireball", &fireball, NULL},
- {"attack", "Water Jet", &water_jet, NULL},
+ {"attack", "Water jet", &water_jet, NULL},
{"attack", "Shield", &shield, NULL},
+ {"attack", "Aerial attack", &ennemie_attack, NULL},
{NULL, NULL, NULL, NULL}
};
diff --git a/src/player_utilities.c b/src/player_utilities.c
index aae16b3..0f299d2 100644
--- a/src/player_utilities.c
+++ b/src/player_utilities.c
@@ -5,19 +5,22 @@
** game_loader
*/
+#include
#include "components//health_component.h"
#include "entity.h"
-void set_combat_player(gc_entity *main_player, gc_entity *combat_player)
+void set_combat_player(gc_engine *engine, gc_entity *player, gc_entity *cbt)
{
- struct health_component *h_cmp_main = \
-GETCMP(main_player, health_component);
- struct health_component *h_cmp_combat = \
-GETCMP(combat_player, health_component);
+ struct health_component *main = GETCMP(player, health_component);
+ struct health_component *combat = GETCMP(cbt, health_component);
+ struct combat_manager *this = GETSYS(engine, combat_manager);
- if (!h_cmp_main || !h_cmp_combat)
+ this->last_attack = NULL;
+ this->last_damage = 0;
+ if (!main || !combat)
return;
- h_cmp_combat->health_max = h_cmp_main->health_max;
- h_cmp_combat->health = h_cmp_main->health;
- h_cmp_combat->dead = h_cmp_main->dead;
+ combat->health_max = main->health_max;
+ combat->health = main->health;
+ combat->dead = main->dead;
+
}
\ No newline at end of file
diff --git a/src/systems/combat_manager.c b/src/systems/combat_manager.c
index 625e20e..843b142 100644
--- a/src/systems/combat_manager.c
+++ b/src/systems/combat_manager.c
@@ -38,7 +38,7 @@ void combat_end(gc_engine *engine, bool has_won)
if (!this->current_enemy || !player || ! player_combat || !dialog)
return;
- set_combat_player(player_combat, player);
+ set_combat_player(engine, player_combat, player);
this->current_enemy = NULL;
engine->change_scene(engine, this->game_scene);
engine->trigger_event(engine, "combat_ended", this->current_enemy, has_won);
diff --git a/src/systems/combat_methods.c b/src/systems/combat_methods.c
index ae03d46..57f4ed4 100644
--- a/src/systems/combat_methods.c
+++ b/src/systems/combat_methods.c
@@ -7,6 +7,7 @@
#include
#include
+#include
#include "systems/combat_manager.h"
#include "tile.h"
#include "prefab.h"
@@ -34,7 +35,7 @@ void combat_start(gc_engine *engine, char *enemy_name)
}
this->state = ATTACK;
this->game_scene = engine->scene;
- set_combat_player(player, player_combat);
+ set_combat_player(engine, player, player_combat);
engine->scene = NULL;
engine->change_scene(engine, scene);
load_attacks(scene);
@@ -119,12 +120,16 @@ void defend_callback(gc_engine *engine)
struct combat_manager *this = GETSYS(engine, combat_manager);
gc_entity *player = NULL;
gc_entity *enemy = NULL;
+ struct renderer *rend;
if (!get_player_and_enemy(engine->scene, &player, &enemy))
return;
+ rend = GETCMP(player, renderer);
+ if (rend)
+ rend_set_anim(rend, "none");
if (GETCMP(enemy, health_component)->dead)
combat_end(engine, true);
- if (this->next_enemy_attack->run)
+ else if (this->next_enemy_attack->run)
this->next_enemy_attack->run(engine, enemy, player);
this->state = ATTACK;
}