Mihael Peklar avatar Mihael Peklar committed 52a7f97

Implemented DPS-based damage calculation and unit death.

Comments (0)

Files changed (5)

 
 Pathfinder* GM_Battle::getPathfinder()  {return this->map->getPathfinder();}
 
+void GM_Battle::removeUnit(Unit* x)
+{
+	if(this->selectedUnit==x)
+		this->selectedUnit=NULL;
+}
+
 /*
 void GM_Battle::_paintSelector(float _x, float _y, float w, float h)
 {
 	void updateSelf(float k);
 	void paintSelf();
 	void handleEvent(SDL_Event &event);
+	void removeUnit(Unit*);
 	Pathfinder* getPathfinder() ;
 
 	private:
 
 Unit::Unit()
 {
-	printf("game: %d\n", game);
 	this->pathfinder = ((GM_Battle*)game)->getPathfinder(); //TODO: There has to be a better way to do this!
 	this->tex = TextureManager::getInstance()->getTexture("zombie.png");
 	this->attacking_tex = TextureManager::getInstance()->getTexture("zombie_attacking.png");
 	this->y = 0;
 
 	this->range=100;
+	this->hp=5;
+	this->dps=1;
+	this->killme=0;
 
 	this->currentlySelected = false;
 	this->activeState = STATE_STANDING;
 
 Unit::Unit(Pathfinder* x)
 {
-	printf("game: %d\n", game);
 	this->pathfinder = x;
 	this->tex = TextureManager::getInstance()->getTexture("zombie.png");
 	this->attacking_tex = TextureManager::getInstance()->getTexture("zombie_attacking.png");
 	this->y = 0;
 
 	this->range=100;
+	this->hp=5;
+	this->dps=1;
+	this->killme=0;
 
 	this->currentlySelected = false;
 	this->activeState = STATE_STANDING;
 Unit::~Unit()
 {
 	disappear();
-	delete this->tex;
+	//delete this->tex; //TODO: implement texture reference counting
 }
 
 // texture directions:
 void Unit::updateSelf(float k)
 {
 	selectTime+=k;
+	if(this->hp<=0)
+	{
+		killme=1;
+		return;
+	}
 
 	switch(activeOrder.order)
 	{
 			if(angle < 0)
 				newDir = 7;
 
+			target->takeDamage(this->dps*k);
+
 			this->dir = circleToDir(newDir);
 		}
 		break;
 	}
 }
 
+void Unit::takeDamage(float x)
+{
+	this->hp-=x;
+	if(this->hp<=0)
+		this->killme=1;
+}
+
+bool Unit::isAlive()
+{
+	return !this->killme;
+}
+
 bool Unit::checkRange(Unit* targ)
 {
 	if(!this->reg.count(targ))
 	void unWatch(Unit*); //also stops order pertaining to chosen unit
 	void disappear(); //removes unit from all watches
 
+	bool isAlive();
+	void takeDamage(float);
 
 	private:
 	void nextOrder();
 	Texture *selector;
 	Texture *attacking_tex;
 	
-	float range;
+	float range, hp, dps;
 	bool checkRange(Unit* targ);
 
 	float aniProgress;
 	Unit::State activeState;
 	Order activeOrder;
 	OrderList list;
+	bool killme;
 
 	Pathfinder* pathfinder; //to get in range again.
 

unitcontainer.cpp

 #include "map.h"
 #include <algorithm>
 #include "font.h"
+#include "gm_battle.h"
+
+using namespace std;
 
 extern Font* test;
 
 
 void UnitContainer::updateSelf(float k)
 {
+	vector<UnitContainer::Container::iterator> toErase;
 	for(UnitContainer::Container::iterator it = units.begin(); it != units.end(); it++)
 	{
 		Unit *unit = *it;
 		unit->updateSelf(k);
+		if(!unit->isAlive())
+		{
+			toErase.push_back(it);
+		}
+	}
+	for(std::vector<UnitContainer::Container::iterator>::iterator it=toErase.begin(); it<toErase.end(); ++it)
+	{
+		delete(**it);
+		((GM_Battle*)game)->removeUnit(**it); ///TODO: There has to be a better way to do this!
+		units.erase(*it);
 	}
 	std::sort(units.begin(), units.end(), Unit::distanceCompare);
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.