git repos / saland

commit 2cc734f6

sago007 · 2020-07-05 08:47
2cc734f6ee015a7528fff10c21fe40876416f32f patch · browse files
parent 7f4f31d55445afe1402614935851030e667549a3

Items now have health and are destructable

Changed files

M src/saland/Game.cpp before
M src/saland/GameItems.cpp before
M src/saland/GameItems.hpp before
M src/saland/GameRegion.cpp before
M src/saland/GameUpdates.cpp before
M src/saland/model/placeables.hpp before
diff --git a/src/saland/Game.cpp b/src/saland/Game.cpp index c77f6ab..161b0c3 100644 --- a/src/saland/Game.cpp +++ b/src/saland/Game.cpp
@@ -467,6 +467,12 @@ void Game::Update() {
if (monster) {
UpdateMonster(monster);
}
+ MiscItem* item = dynamic_cast<MiscItem*> (entity.get());
+ if (item) {
+ if (item->destructible && item->health <= 0.0f && !item->removeMe) {
+ item->removeMe = true;
+ }
+ }
}
auto& vp = data->gameRegion.placeables;
vp.erase(std::remove_if(std::begin(vp), std::end(vp), [](std::shared_ptr<Placeable> p) {
diff --git a/src/saland/GameItems.cpp b/src/saland/GameItems.cpp index 7d411b2..f13aedc 100644 --- a/src/saland/GameItems.cpp +++ b/src/saland/GameItems.cpp
@@ -27,6 +27,8 @@ static ItemDef itemDef;
const ItemDef& getItem(const std::string& itemName) {
itemDef.sprite2 = "";
+ itemDef.isDestructible = true;
+ itemDef.health = 100.0f;
if (itemName == "barrel") {
itemDef.radius = 16.0f;
itemDef.sprite = "item_barrel";
@@ -34,6 +36,7 @@ const ItemDef& getItem(const std::string& itemName) {
itemDef.isStatic = true;
}
if (itemName == "food_potato") {
+ itemDef.isDestructible = false;
itemDef.radius = 9.0f;
itemDef.sprite = "item_food_potato";
itemDef.isStatic = false;
diff --git a/src/saland/GameItems.hpp b/src/saland/GameItems.hpp index 343e33d..03ef9be 100644 --- a/src/saland/GameItems.hpp +++ b/src/saland/GameItems.hpp
@@ -32,6 +32,8 @@ struct ItemDef {
std::string sprite = "";
std::string sprite2 = "";
bool isStatic = true;
+ bool isDestructible = true;
+ float health = 10.0f;
};
const ItemDef& getItem(const std::string& itemName);
diff --git a/src/saland/GameRegion.cpp b/src/saland/GameRegion.cpp index ab12e00..bf52567 100644 --- a/src/saland/GameRegion.cpp +++ b/src/saland/GameRegion.cpp
@@ -67,6 +67,8 @@ void GameRegion::SpawnItem(const ItemDef& def, float destX, float destY) {
barrel.get()->sprite2 = def.sprite2;
barrel.get()->X = destX;
barrel.get()->Y = destY;
+ barrel.get()->destructible = def.isDestructible;
+ barrel.get()->health = def.health;
placeables.push_back(barrel);
if (def.isStatic) {
diff --git a/src/saland/GameUpdates.cpp b/src/saland/GameUpdates.cpp index aa5f3ed..dfeba10 100644 --- a/src/saland/GameUpdates.cpp +++ b/src/saland/GameUpdates.cpp
@@ -101,9 +101,9 @@ void UpdateProjectile(Projectile* entity, float fDeltaTime) {
}
void ProjectileHit(Projectile* p, Placeable* target) {
- Monster* monster = dynamic_cast<Monster*> (target);
- if (monster) {
- monster->health -= 10.0f;
+ //Monster* monster = dynamic_cast<Monster*> (target);
+ if (target->destructible) {
+ target->health -= 10.0f;
p->removeMe = true;
}
}
\ No newline at end of file
diff --git a/src/saland/model/placeables.hpp b/src/saland/model/placeables.hpp index 21b4597..8208c36 100644 --- a/src/saland/model/placeables.hpp +++ b/src/saland/model/placeables.hpp
@@ -31,10 +31,12 @@ const float pixel2unit = 32.0f;
class Placeable {
public:
+ float health = 10.0;
float X = 20.0;
float Y = 20.0;
float Radius = 16.0;
bool removeMe = false;
+ bool destructible = true;
b2Body* body = nullptr;
virtual bool isStatic() {
return true;
@@ -52,7 +54,6 @@ public:
class Creature : public Placeable {
public:
- float health = 10.0;
float stinema = 10.0;
float mana = 10.0;
char direction = 'S';