- Timestamp:
- Apr 23, 2013, 1:31:54 AM (12 years ago)
- Branches:
- master
- Children:
- 227baaa
- Parents:
- 054b50b
- Location:
- common
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
common/Player.cpp
r054b50b ra1a3bd5 83 83 } 84 84 85 void Player::move(void) {85 bool Player::move(WorldMap map) { 86 86 int speed = 100; // pixels per second 87 87 unsigned long long curTime = getCurrentMillis(); 88 bool moveCanceled = false; 88 89 89 90 // if we're at our target, don't move 90 91 if (pos.x != target.x || pos.y != target.y) { 91 92 float pixels = speed * (curTime-timeLastUpdated) / 1000.0; 93 double angle = atan2(target.y-pos.y, target.x-pos.x); 94 float dist = sqrt(pow(target.x-pos.x, 2) + pow(target.y-pos.y, 2)); 95 POSITION newPos; 92 96 93 double angle = atan2(target.y-pos.y, target.x-pos.x);94 95 float dist = sqrt(pow(target.x-pos.x, 2) + pow(target.y-pos.y, 2));96 97 if (dist <= pixels) { 97 98 pos.x = target.x; 98 99 pos.y = target.y; 99 100 }else { 100 pos.x += cos(angle)*pixels; 101 pos.y += sin(angle)*pixels; 101 newPos.x = int(pos.x + cos(angle)*pixels); 102 newPos.y = int(pos.y + sin(angle)*pixels); 103 104 switch(map.getElement(newPos.x/25, newPos.y/25)) { 105 case WorldMap.TerrainType.TERRAIN_OCEAN: 106 case WorldMap.TerrainType.TERRAIN_ROCK: 107 target.x = pos.x; 108 target.y = pos.y; 109 moveCanceled = true; 110 break; 111 } 102 112 } 103 113 } 104 114 105 115 timeLastUpdated = curTime; 116 return !moveCanceled; 106 117 } -
common/Player.h
r054b50b ra1a3bd5 14 14 15 15 #include "Common.h" 16 #include "WorldMap.h" 16 17 17 18 using namespace std; … … 32 33 void setAddr(sockaddr_in addr); 33 34 34 void move();35 bool move(WorldMap map); 35 36 36 37 int id; -
common/WorldMap.cpp
r054b50b ra1a3bd5 15 15 16 16 vctMap = new vector<vector<TerrainType>*>(width); 17 vctObjects = new vector<vector<ObjectType>*>(width); 17 18 18 19 for (int x=0; x<width; x++) { 19 vector<TerrainType>* newVector = new vector<TerrainType>(height); 20 vector<TerrainType>* newMapVector = new vector<TerrainType>(height); 21 vector<ObjectType>* newObjectVector = new vector<ObjectType>(height); 20 22 21 for (int y=0; y<height; y++) 22 (*newVector)[y] = TERRAIN_NONE; 23 for (int y=0; y<height; y++) { 24 (*newMapVector)[y] = TERRAIN_NONE; 25 (*newObjectVector)[y] = OBJECT_NONE; 26 } 23 27 24 (*vctMap)[x] = newVector; 28 (*vctMap)[x] = newMapVector; 29 (*vctObjects)[x] = newObjectVector; 25 30 } 26 31 } … … 28 33 WorldMap::~WorldMap() 29 34 { 30 for (int x=0; x<width; x++) 35 for (int x=0; x<width; x++) { 31 36 delete (*vctMap)[x]; 37 delete (*vctObjects)[x]; 38 } 32 39 33 40 delete vctMap; 41 delete vctObjects; 34 42 } 35 43 … … 43 51 cout << "getting element" << endl; 44 52 (*(*vctMap)[x])[y] = t; 53 } 54 55 WorldMap::ObjectType WorldMap::getObject(int x, int y) 56 { 57 return (*(*vctObjects)[x])[y]; 58 } 59 60 void WorldMap::setObject(int x, int y, ObjectType t) 61 { 62 cout << "getting object" << endl; 63 (*(*vctObjects)[x])[y] = t; 45 64 } 46 65 … … 57 76 else 58 77 m->setElement(x, y, TERRAIN_GRASS); 78 79 m->setObject(x, y, OBJECT_NONE); 59 80 } 60 81 } … … 102 123 istringstream iss(line); 103 124 string token; 104 int type;105 TerrainType terrain;106 125 107 for(int x=0; x<width; x++) 108 { 126 if (row < height) { 127 // load terrain 128 129 int type; 130 TerrainType terrain; 131 132 for(int x=0; x<width; x++) 133 { 134 getline(iss, token, ','); 135 cout << "token: " << token << endl; 136 type = atoi(token.c_str()); 137 cout << "type: " << type << endl; 138 139 switch(type) { 140 case 1: 141 terrain = TERRAIN_GRASS; 142 break; 143 case 2: 144 terrain = TERRAIN_OCEAN; 145 break; 146 case 3: 147 terrain = TERRAIN_ROCK; 148 break; 149 } 150 151 cout << "About to set element" << endl; 152 cout << "x: " << x << endl; 153 cout << "row: " << row << endl; 154 m->setElement(x, row, terrain); 155 } 156 }else { 157 // load objects 158 159 int x, y, type; 160 ObjectType object; 161 109 162 getline(iss, token, ','); 110 cout << "token: " << token << endl; 163 cout << "token(x): " << token << endl; 164 x = atoi(token.c_str()); 165 166 getline(iss, token, ','); 167 cout << "token(y): " << token << endl; 168 y = atoi(token.c_str()); 169 170 getline(iss, token, ','); 171 cout << "token(type): " << token << endl; 111 172 type = atoi(token.c_str()); 112 cout << "type: " << type << endl;113 173 114 174 switch(type) { 175 case 0: 176 object = OBJECT_NONE; 177 break; 115 178 case 1: 116 terrain = TERRAIN_GRASS;179 object = OBJECT_RED_FLAG; 117 180 break; 118 181 case 2: 119 terrain = TERRAIN_OCEAN; 120 break; 121 case 3: 122 terrain = TERRAIN_ROCK; 182 object = OBJECT_BLUE_FLAG; 123 183 break; 124 184 } 125 185 126 cout << "About to set element" << endl; 127 cout << "x: " << x << endl; 128 cout << "row: " << row << endl; 129 m->setElement(x, row, terrain); 186 m->setObject(x, y, object); 130 187 } 131 188 } -
common/WorldMap.h
r054b50b ra1a3bd5 17 17 }; 18 18 19 enum ObjectType { 20 OBJECT_NONE, 21 OBJECT_RED_FLAG, 22 OBJECT_BLUE_FLAG 23 }; 24 19 25 int width, height; 20 26 vector<vector<TerrainType>*>* vctMap; 27 vector<vector<ObjectType>*>* vctObjects; 21 28 22 29 WorldMap(int width, int height); … … 27 34 void setElement(int x, int y, TerrainType type); 28 35 36 ObjectType getObject(int x, int y); 37 void setObject(int x, int y, ObjectType type); 38 29 39 static WorldMap* createDefaultMap(); 30 40 static WorldMap* loadMapFromFile(string filename);
Note:
See TracChangeset
for help on using the changeset viewer.