Commits

Mihael Peklar committed 412e4b2

Moved units-related code from map AND gm_battle to a new class!

  • Participants
  • Parent commits 253454d

Comments (0)

Files changed (7)

 CFLAGS=$(MY_CFLAGS)
 CXXFLAGS=$(MY_CXXFLAGS)
 LDFLAGS=-lSDL -lSDL_mixer -lSDL_image -lSDL_ttf -lGL -lGLU $(MY_LDFLAGS)
-OBJECTS=audiomgr.o    gamemode.o    gm_battle.o   texture.o  map.o tile.o tileset.o thecombat.o unit.o font.o button.o tiledesc.o order.o
+OBJECTS=audiomgr.o    gamemode.o    gm_battle.o   texture.o  map.o tile.o tileset.o thecombat.o unit.o font.o button.o tiledesc.o order.o unitcontainer.o
 
 all: thecombat
 

File gm_battle.cpp

 #include "map.h"
 #include "font.h"
 #include "button.h"
+#include "unitcontainer.h"
 
 #define TESTING
 
 	this->offsetX = this->offsetY = 0;
 	Unit *unit = NULL;
     this->map = new Map("skirmish1.thecombatmap");
+    this->units = this->map->getUnits();
+    this->selectedUnit=NULL;
 
 	unit = new Unit();
-	this->map->addUnit(unit);
+	this->units->addUnit(unit);
 	unit = new Unit();
 	unit->setX(90);
 	unit->setY(10);
-	this->map->addUnit(unit);
+	this->units->addUnit(unit);
 
 	btnTest = new Button();
 	btnTest->setTexture(Button::STATE_UP,"btnUp.png");
     glTranslatef(offsetX, offsetY, 0);
     glPushMatrix();
     this->map->paintSelf();
+    this->units->paintSelf();
 	btnTest->paintSelf();
 	glPopMatrix();
 	//glOrtho(0, 0, this->adjustedWidth(), this->adjustedHeight(), -1, 1);
 	this->gameTime += k;
 
 	btnTest->updateSelf();
-	map->updateSelf(k);
+	units->updateSelf(k);
 
     float scrollSpeed = 502;
     if(mousePositionX < -this->adjustedWidth()/2 + 32)
 			{
 				case SDL_BUTTON_LEFT:
 				{
-					bool t=true;
-					for(std::vector<Unit*>::iterator it = this->map->units.begin(); it != this->map->units.end(); it++) //TODO: Move this to UnitContainer
-					{
-						Unit *unit = *it;
-						if(unit->wasHit(adjustedMouseX, adjustedMouseY) && t)
-						{
-							unit->_setSelected(true);
-							t=false;
-						}
-						else
-						{
-							unit->_setSelected(false);
-						}
-					}
-					btnTest->pressed();
+					this->selectedUnit=units->selectUnitByCoord(adjustedMouseX, adjustedMouseY);
 					break;
 				}
 				case SDL_BUTTON_RIGHT:
 				{
-					for(Map::UnitContainer::iterator it = this->map->units.begin(); it != this->map->units.end(); it++)
+					if(this->selectedUnit!=NULL)
 					{
-						Unit *unit = *it;
-						if(unit->isSelected())
-						{
-							unit->setOrders(this->map->path_order(unit->getX(), unit->getY(), adjustedMouseX, adjustedMouseY));
-						}
+						Unit* unit=this->selectedUnit;
+						this->selectedUnit->setOrders(this->map->path_order(unit->getX(), unit->getY(), adjustedMouseX, adjustedMouseY));
 					}
 					break;
 				}
 class Texture;
 class Map;
 class Button;
+class UnitContainer;
 
 class GM_Battle : public GameMode
 {
 	void handleEvent(SDL_Event &event);
 
 	private:
-		
-		
-	std::vector<Unit*> units;
+	Unit* selectedUnit;
 	Map *map;
+	UnitContainer* units;
 	float gameTime;
 	float mousePositionX, mousePositionY;
 	Button *btnTest;
 #include<utility>
 #include<queue>
 #include"order.h"
+#include"unitcontainer.h"
 
 #define INVALID_MAP { \
         fclose(f); \
     }
     fclose(f);
     blocked.resize(tiles.size(), 0);
+    units=new UnitContainer(this);
 }
 
 Map::~Map()
     {
         delete *it;
     }
-    for(UnitContainer::iterator it=units.begin(); it!=units.end(); ++it)
-    	delete *it;
+    delete units;
 }
 
-void Map::addUnit(Unit* x)
+UnitContainer* Map::getUnits()
 {
-	int c=this->findTile(x->getX(), x->getY());
-	if(c<0 || c>=this->tiles.size())
-		return;
-	units.push_back(x);
-	blocked[c]=1;
-	//std::sort(units.begin(), units.end(), Unit::distanceCompare);
-}
-
-void Map::updateSelf(float k)
-{
-	for(UnitContainer::iterator it = units.begin(); it != units.end(); it++)
-	{
-		Unit *unit = *it;
-		unit->updateSelf(k);
-	}
-	std::sort(units.begin(), units.end(), Unit::distanceCompare);
+	return this->units;
 }
 
 void Map::paintSelf()
         Tile *t = this->tiles[i];
         t->paintSelf(pixelX, -pixelY);
     }
-    for(UnitContainer::iterator it = units.begin(); it != units.end(); it++)
-	{
-		Unit *unit = *it;
-		/*if(unit->isSelected())
-		{
-			unit->_paintSelector(unit->getX(), unit->getY() + 16, unit->getWidth(), unit->getHeight());
-		}*/
-		test->drawCenteredLine("Zombie", unit->getX(), unit->getY()+unit->getHeight());
-		unit->paintSelf();
-	}
+}
+
+bool Map::boundCheck(float x, float y)
+{
+	x+=TILE_WIDTH/2; y-=TILE_HEIGHT/2;
+	y*=-1;
+	if(x<0)
+		return 0;
+	if(y<0)
+		return 0;
+	if(x>(this->tilesPerRow-1)*TILE_WIDTH)
+		return 0;
+	if(y>(this->tilesPerColumn-1)*TILE_HEIGHT)
+		return 0;
+	return 1;
+
 }
 
 int Map::findTile(float x, float y)
 	return x/TILE_WIDTH + (int)((y/TILE_HEIGHT))*this->tilesPerRow;
 }
 
+void Map::blockTile(int x)
+{
+	blocked[x]=1;
+}
+
 void Map::boundFix(float& x, float& y, float offX, float offY)
 {
 	x+=offX; y-=offY;
 class Unit;
 class Order;
 class Tile;
+class UnitContainer;
 
 class Map
 {
     public:
-    typedef std::vector<Unit*> UnitContainer;
     Map(const std::string &filename);
     ~Map();
-    void addUnit(Unit*);
     void paintSelf();
-    void updateSelf(float);
     double tileSpeed(int x, int y);
     int getTilesPerRow() {return this->tilesPerRow;}
     int getTilesPerColumn() {return this->tilesPerColumn;}
+    bool boundCheck(float x, float y);
     void boundFix(float& x, float& y, float offX=TILE_WIDTH/2, float offY=TILE_HEIGHT/2);
-    UnitContainer units;
     std::vector<int> find_path(int st, int et);
     std::deque<Order> path_order(float px, float py, float ex, float ey);
     int findTile(float x, float y);
+    void blockTile(int x);
     void drawPath(std::vector<int> path);
     void drawPath();
+    UnitContainer* getUnits();
 
     private:
+    UnitContainer* units;
     Tileset* tileset;
     double distance(int, int);
     bool siri(int x, int y);

File unitcontainer.cpp

+#include "unitcontainer.h"
+
+#include "unit.h"
+#include "map.h"
+#include <algorithm>
+#include "font.h"
+
+extern Font* test;
+
+UnitContainer::UnitContainer(Map* x) : map(x) {}
+
+UnitContainer::~UnitContainer()
+{
+	for(UnitContainer::Container::iterator it=units.begin(); it!=units.end(); ++it)
+		delete *it;
+}
+
+void UnitContainer::addUnit(Unit* x)
+{
+	bool c=this->map->boundCheck(x->getX(), x->getY());
+	if(c==0)
+		return;
+	units.push_back(x);
+	map->blockTile(this->map->findTile(x->getX(), x->getY()));
+	//std::sort(units.begin(), units.end(), Unit::distanceCompare);
+}
+
+void UnitContainer::updateSelf(float k)
+{
+	for(UnitContainer::Container::iterator it = units.begin(); it != units.end(); it++)
+	{
+		Unit *unit = *it;
+		unit->updateSelf(k);
+	}
+	std::sort(units.begin(), units.end(), Unit::distanceCompare);
+}
+
+void UnitContainer::paintSelf()
+{
+    for(UnitContainer::Container::iterator it = units.begin(); it != units.end(); it++)
+	{
+		Unit *unit = *it;
+		/*if(unit->isSelected())
+		{
+			unit->_paintSelector(unit->getX(), unit->getY() + 16, unit->getWidth(), unit->getHeight());
+		}*/
+		test->drawCenteredLine("Zombie", unit->getX(), unit->getY()+unit->getHeight());
+		unit->paintSelf();
+	}
+}
+
+Unit* UnitContainer::selectUnitByCoord(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)
+		{
+			unit->_setSelected(true);
+			t=unit;
+		}
+		else
+		{
+			unit->_setSelected(false);
+		}
+	}
+	return t;
+}
+
+
+

File unitcontainer.h

+#ifndef UNITCONTAINER_H_INCLUDED
+#define UNITCONTAINER_H_INCLUDED
+
+#include<vector>
+class Unit;
+class Map;
+
+class UnitContainer
+{
+	public:
+	typedef std::vector<Unit*> Container;
+	UnitContainer(Map*);
+	~UnitContainer();
+    void addUnit(Unit*);
+    void paintSelf();
+    void updateSelf(float);
+    Unit* selectUnitByCoord(float x, float y); //TODO: Hack up something more versatile
+
+	private:
+	Map* map;
+	Container units;
+};
+
+#endif
+