Mihael Peklar avatar Mihael Peklar committed 8a99264

Started work on Attack

Comments (0)

Files changed (8)

 					if(this->selectedUnit!=NULL)
 					{
 						Unit* unit=this->selectedUnit;
-						this->selectedUnit->setOrders(this->map->path_order(unit->getX(), unit->getY(), adjustedMouseX, adjustedMouseY));
+						Unit* target;
+						if(NULL==(target=this->units->getUnitByCoord(adjustedMouseX, adjustedMouseY)))
+							this->selectedUnit->setOrders(this->map->path_order(unit->getX(), unit->getY(), adjustedMouseX, adjustedMouseY));
+						else //TODO: Do this the right way!
+						{
+							this->selectedUnit->attackOrder(target);
+						}
 					}
 					break;
 				}
 enum OrderType
 {
 	ORDER_NONE=0,
-	ORDER_MOVE
+	ORDER_MOVE,
+	ORDER_ATTACK,
 };
 
+class Unit;
+
 union OrderDetails
 {
 	struct OrderDetailsMove
 		float dstx, dsty;
 		float progress;
 	} move;
+	struct OrderDetailsAttack
+	{
+		Unit* target;
+	} attack;
+	
 };
 
 class Order
 Unit::Unit()
 {
 	this->tex = TextureManager::getInstance()->getTexture("zombie.png");
+	this->attacking_tex = TextureManager::getInstance()->getTexture("zombie_attacking.png");
+
 	this->selector = TextureManager::getInstance()->getTexture("selector.png");
 	this->aniProgress = 0;
 	this->dir = 6;
 	x += (this->dir % 3) * 4*32;
 	y += (this->dir / 3) * 4*32;
 
-	this->tex->paintSubImageWithPixels(x,y,32,32);
+	switch(this->activeState)
+	{
+		case STATE_ATTACKING:
+			this->attacking_tex->paintSubImageWithPixels(x,y,32,32);
+		break;
+		default:
+			this->tex->paintSubImageWithPixels(x,y,32,32);
+	}
 
 	glPopMatrix();
 }
 	switch(activeOrder.order)
 	{
 		case ORDER_NONE:
-		activeState = STATE_STANDING;
+		{
+			activeState = STATE_STANDING;
+		}
 		break;
 
 		case ORDER_MOVE:
 		switch(activeState)
 		{
 			case STATE_STANDING:
+			case STATE_ATTACKING:
 			activeState = STATE_MOVING;
 			break;
 
 			default:
 			break;
 		}
+		break;
+		case ORDER_ATTACK:
+		{
+			activeState = STATE_ATTACKING;
+			//assert(0);
+			break;	
+		}
 	}
 
 	switch(activeState)
 			}
 		}
 		break;
+		
+		case STATE_ATTACKING:
+		{
+			Unit* target=this->activeOrder.details.attack.target;
+
+			float speed = 70;
+
+			this->aniProgress += speed / 5 * k;
+
+			float dX = target->x - this->x;
+			float dY = target->y - this->y;
+
+
+			float angle=atan2(-dY, dX);
+			angle *= 180. / 3.14159;
+			angle += 180;
+			angle -= (360./8.)/2;
+			int newDir = int(angle / (360./8.)) % 8;
+			if(angle < 0)
+				newDir = 7;
+
+			this->dir = circleToDir(newDir);
+		}
+		break;
 
 	}
 
 	this->movementOrder(mapX, mapY);
 }
 
+void Unit::attackOrder(Unit* target)
+{
+	this->activeState=STATE_ATTACKING;
+	this->activeOrder.order=ORDER_ATTACK;
+	this->activeOrder.details.attack.target=target;
+}
+
 void Unit::setOrders(OrderList x)
 {
 	this->list=x;
 	{
 		STATE_STANDING=0,
 		STATE_MOVING,
+		STATE_ATTACKING,
 	};
 
 	Unit();
 	int getActiveState() const { return activeState; }
 
 
-	// orders:
+	//orders
+	void setOrders(OrderList x);
+	// obsolete:
 	void defaultOrder(float mapX, float mapY);
 	void movementOrder(float mapX, float mapY);
-	void setOrders(OrderList x);
+	void attackOrder(Unit*);
 	// debug only:
 	void _setSelected(bool selection) { currentlySelected = selection; }
 
 	static int circleToDir(int);
 	Texture *tex;
 	Texture *selector;
+	Texture *attacking_tex;
 	
 	float aniProgress;
 	int dir;

unitcontainer.cpp

 }
 
 
+Unit* UnitContainer::getUnitByCoord(float x, float y)
+{
+	Unit* t=NULL;
+	for(std::vector<Unit*>::iterator it = this->units.begin(); it != this->units.end(); it++)
+	{
+		Unit *unit = *it;
+		if(unit->wasHit(x, y) && t==NULL)
+		{
+			t=unit;
+			break;
+		}
+	}
+	return t;
+}
 
+
+
     void paintSelf();
     void updateSelf(float);
     Unit* selectUnitByCoord(float x, float y); //TODO: Hack up something more versatile
+    Unit* getUnitByCoord(float x, float y);
 
 	private:
 	Map* map;
Added
New image
Add a comment to this file

zombie_taking_damage.png

Added
New image
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.