Google
Web alhem.net

Map.cpp

Go to the documentation of this file.
00001 //#include <stdio.h>
00002 #include <math.h>
00003 #include "Robot.h"
00004 #include "Powerup.h"
00005 #include "Item.h"
00006 #include "Map.h"
00007 
00008 
00009 Map::Map()
00010 :m_max_x(0)
00011 ,m_max_y(0)
00012 ,m_wraps(false)
00013 {
00014 }
00015 
00016 
00017 Map::~Map()
00018 {
00019         for (mapobject_v::iterator it = m_objects.begin(); it != m_objects.end(); it++)
00020         {
00021                 MapObject *p = *it;
00022                 if (p -> DeleteByMap())
00023                 {
00024                         delete p;
00025                 }
00026         }
00027 }
00028 
00029 
00030 void Map::SetMax(int x,int y)
00031 {
00032         m_max_x = x;
00033         m_max_y = y;
00034 }
00035 
00036 
00037 void Map::AddObject(MapObject *p,bool delete_by_map)
00038 {
00039         m_objects.push_back(p);
00040         p -> SetDeleteByMap(delete_by_map);
00041         printf("New object added - new map size is %d object%s\n",m_objects.size(),(m_objects.size() == 1) ? "" : "s");
00042 }
00043 
00044 
00045 void Map::IncreaseTime(long usec)
00046 {
00047         for (mapobject_v::iterator it = m_objects.begin(); it != m_objects.end(); it++)
00048         {
00049                 MapObject *p0 = *it;
00050                 Robot *p = dynamic_cast<Robot *>(p0);
00051                 if (p)
00052                 {
00053                         p -> Tick( usec );
00054                         p -> Call("Main");
00055                 }
00056         }
00057 }
00058 
00059 
00060 void Map::SpawnRandom()
00061 {
00062         MapObject *p = NULL;
00063         switch (random() % 2)
00064         {
00065         case 0: // powerup
00066                 p = new Powerup( *this );
00067                 break;
00068         case 1: // item
00069                 p = new Item( *this );
00070                 break;
00071         }
00072         p -> SetPos(random() % m_max_x,random() % m_max_y);
00073         double dist = GetNearest(p -> GetX(),p -> GetY());
00074         if (dist > 20)
00075         {
00076                 m_objects.push_back( p );
00077         }
00078         else
00079         {
00080                 delete p;
00081         }
00082 }
00083 
00084 
00085 double Map::GetNearest(double x,double y)
00086 {
00087         double dist = m_max_x * m_max_y;
00088         for (mapobject_v::iterator it = m_objects.begin(); it != m_objects.end(); it++)
00089         {
00090                 MapObject *p = *it;
00091                 double x1 = p -> GetX() - x;
00092                 double x2 = x - p -> GetX();
00093                 if (x1 < m_max_x)
00094                         x1 += m_max_x;
00095                 if (x2 < m_max_x)
00096                         x2 += m_max_x;
00097                 double y1 = p -> GetY() - y;
00098                 double y2 = y - p -> GetY();
00099                 if (y1 < m_max_y)
00100                         y1 += m_max_y;
00101                 if (y2 < m_max_y)
00102                         y2 += m_max_y;
00103                 double xx = (x1 < x2) ? x1 : x2;
00104                 double yy = (y1 < y2) ? y1 : y2;
00105                 double d = sqrt(xx * xx + yy * yy);
00106                 if (d < dist)
00107                         dist = d;
00108         }
00109         return dist;
00110 }
00111 
00112 

Generated for Robot World by doxygen 1.3.6

Page, code, and content Copyright (C) 2004 by Anders Hedström