Here we are after long consideration of the anchor point and solved some inconsistencies, I decided it would be best to make the anchor point at (0,0).
New Stuff
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#pragma once #include "cocos2d.h" using namespace cocos2d; class Player : public Object { public: //FIELDS enum State { Standing, Walking, Jumping }; State state; bool facingRight; bool grounded; float stateTime; Size player_size; Point position; Point velocity; Player(void); virtual ~Player(void); }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#pragma once #include "cocos2d.h" #include <vector> using namespace std; using namespace cocos2d; class Level : public Object { public: TMXTiledMap *map; void loadMap(const char* name); TMXTiledMap * getMap(); Point tileCoordinateToPosition(Point point); Point positionToTileCoordinate(Point point); vector<Rect> getCollisionTiles(Point point, int fromX, int fromY); Level(void); virtual ~Level(void); }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
#include "Level.h" #include "globals.h" void Level::loadMap(const char* mapname){ map = TMXTiledMap::create(mapname); map->retain(); } TMXTiledMap * Level::getMap(){ return map; } Point Level::tileCoordinateToPosition(Point point){ float x = floor(point.x * map->getTileSize().width * SCALE_FACTOR); float y = floor(point.y * map->getTileSize().height * SCALE_FACTOR); return Point(x, y); } Point Level::positionToTileCoordinate(Point point){ float x = floor(point.x / (map->getTileSize().width * SCALE_FACTOR)); float y = floor(point.y / (map->getTileSize().height * SCALE_FACTOR)); return Point((int)x, (int)y); } vector<Rect> Level::getCollisionTiles(Point point, int fromX, int fromY){ vector<Rect> list; TMXLayer *walls = map->getLayer("walls"); int mapheight = (int)map->getMapSize().height - 1; for (int a = fromX; a < 2; a++) { for (int b = fromY; b < 2; b++) { if(!(a == 10 && b == 10)){ Sprite *tile = walls->getTileAt(Point((int)point.x + a, mapheight - ((int)point.y + b))); if (tile != NULL) { //CCLOG("%d %d",(int)point.x + a, mapheight - ((int)point.y + b)); DrawNode *rectWithBorder = DrawNode::create(); Vec2 vertices[] = { Vec2(0, map->getTileSize().height * SCALE_FACTOR), Vec2(map->getTileSize().width * SCALE_FACTOR, map->getTileSize().height * SCALE_FACTOR), Vec2(map->getTileSize().width * SCALE_FACTOR, 0), Vec2(0,0) }; Point tmp = walls->positionAt(Point((int)point.x + a, mapheight - ((int)point.y + b))); rectWithBorder->setPosition(tmp.x * SCALE_FACTOR, tmp.y * SCALE_FACTOR); Rect tileRect = rectWithBorder->getBoundingBox(); tileRect.setRect(rectWithBorder->getBoundingBox().getMinX(), rectWithBorder->getBoundingBox().getMinY(), map->getTileSize().width * SCALE_FACTOR, map->getTileSize().height * SCALE_FACTOR); list.push_back(tileRect); } } } } return list; } Level::Level(void) { } Level::~Level(void) { map->release(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
#pragma once #include "cocos2d.h" #include "Level.h" #include "Player.h" #include <algorithm> #include <string> #include <vector> using namespace std; USING_NS_CC; class GameScene : public cocos2d::Layer { public: //FIELDS Level * level; Player *player; Sprite *player_sprite; Sprite *cameraTarget; Animate *walkRight; Animate *jumping; Animate *falling; boolean collidesX; Follow *camera; DrawNode *rectWithBorder; vector<EventKeyboard::KeyCode> heldKeys; //CONSTRUCTOR & METHODS virtual void onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event); virtual void onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event); void updateScene(float interval); void updatePlayer(float interval); void updatePlayerSprite(float interval); int signum(float x); static cocos2d::Scene* createScene(); virtual bool init(); void menuCloseCallback(cocos2d::Ref* pSender); void setupAnimations(); CREATE_FUNC(GameScene); GameScene(void); virtual ~GameScene(void); }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
#include "GameScene.h" #include "globals.h" Scene* GameScene::createScene() { auto scene = Scene::create(); auto layer = GameScene::create(); scene->addChild(layer); return scene; } bool GameScene::init() { if ( !Layer::init() ) { return false; } level = new Level(); level->loadMap("level1.tmx"); level->retain(); auto director = Director::getInstance(); level->getMap()->setScale(SCALE_FACTOR); this->addChild(level->getMap()); SpriteFrameCache::getInstance()->addSpriteFramesWithFile("solbrain.plist"); AnimationCache::getInstance()->addAnimationsWithFile("solbrain-animations.plist"); player_sprite = Sprite::createWithSpriteFrameName("idle"); player_sprite->setScale(SCALE_FACTOR); player_sprite->setAnchorPoint(Point(0, 0)); player_sprite->setFlippedX(true); Point point = Point(10,2); player_sprite->setPosition(level->tileCoordinateToPosition(point)); player = new Player(); player->retain(); player->state = Player::State::Standing; player->position = player_sprite->getPosition(); player->player_size.width = player_sprite->getBoundingBox().size.width; player->player_size.height = player_sprite->getBoundingBox().size.height; Point origin = Director::getInstance()->getVisibleOrigin(); Size wsize = Director::getInstance()->getVisibleSize(); //default screen size (or design resolution size, if you are using design resolution) Point *center = new Point(wsize.width/2 + origin.x, wsize.height/2 + origin.y); cameraTarget = Sprite::create(); cameraTarget->setPositionX(player_sprite->getPosition().x); // set to players x cameraTarget->setPositionY(wsize.height/2 + origin.y); // center of height cameraTarget->retain(); this->setupAnimations(); this->addChild(player_sprite); this->schedule(schedule_selector(GameScene::updateScene)); this->addChild(cameraTarget); rectWithBorder = DrawNode::create(); Vec2 vertices[] = { Vec2(0, player_sprite->getBoundingBox().size.height), Vec2(player_sprite->getBoundingBox().size.width, player_sprite->getBoundingBox().size.height), Vec2(player_sprite->getBoundingBox().size.width, 0), Vec2(0,0) }; rectWithBorder->drawPolygon(vertices, 4, Color4F(0.0f,0.3f,0.3f,1), 0, Color4F(0.0f,0.2f,0.0f,1)); addChild(rectWithBorder); camera = Follow::create(cameraTarget, Rect::ZERO); camera->retain(); this->runAction(camera); return true; } void GameScene::setupAnimations(){ AnimationCache *cache = AnimationCache::getInstance(); Animation *animation = cache->getAnimation("walk"); Animate* animate = Animate::create(animation); animate->getAnimation()->setRestoreOriginalFrame(true); animate->setDuration(0.80f); animate->setTarget(player_sprite); this->walkRight = animate; this->walkRight->retain(); } void GameScene::updateScene(float delta){ cameraTarget->setPositionX( player_sprite->getPosition().x ); this->updatePlayer(delta); } void GameScene::updatePlayer(float delta){ if (std::find(heldKeys.begin(), heldKeys.end(), SPACEBAR) != heldKeys.end()) { if(player->grounded){ player->velocity.y = PLAYER_JUMP_VELOCITY; player->state = Player::State::Jumping; player->grounded = false; } } if(std::find(heldKeys.begin(), heldKeys.end(), RIGHT_ARROW) != heldKeys.end()){ player->velocity.x = PLAYER_MAX_VELOCITY; if(player->grounded){ player->state = Player::State::Walking; } player->facingRight = true; } if(std::find(heldKeys.begin(), heldKeys.end(), LEFT_ARROW) != heldKeys.end()){ player->velocity.x = -PLAYER_MAX_VELOCITY; if(player->grounded){ player->state = Player::State::Walking; } player->facingRight = false; } player->velocity -= Point(0, GRAVITY); /////////////////////////////////////////////////////// Rect player_rect = player_sprite->getBoundingBox(); Point tmp; vector<Rect> tiles; tmp = level->positionToTileCoordinate(Point(player->position.x + player->player_size.width * 0.5f , player->position.y + player->player_size.height * 0.5f)); /*if(player->velocity.x >= 0){ tmp = level->positionToTileCoordinate(Point(player->position.x + player->player_size.width * 0.5f , player->position.y + player->player_size.height * 0.5f)); }else { tmp = level->positionToTileCoordinate(Point(player->position.x , player->position.y + player->player_size.width * 0.5f)); }*/ tiles = level->getCollisionTiles(tmp, -1, 0); if(player->velocity.x >= 0){ player_rect.setRect( player_sprite->getBoundingBox().getMinX() + player->velocity.x, player_sprite->getBoundingBox().getMinY(), player->player_size.width, player->player_size.height ); CCLOG("%f",player->player_size.width); }else { player_rect.setRect( player_sprite->getBoundingBox().getMinX() + player->velocity.x, player_sprite->getBoundingBox().getMinY(), player->player_size.width, player->player_size.height ); } for (Rect tile : tiles) { if (player_rect.intersectsRect(tile)) { player->velocity.x = 0; break; } } tiles.clear(); tiles = level->getCollisionTiles(tmp, -1, -1); player_rect.setRect( player_sprite->getBoundingBox().getMinX(), player_sprite->getBoundingBox().getMinY() + player->velocity.y, player->player_size.width, player->player_size.height ); for (Rect tile : tiles) { player->grounded = false; if (tile.intersectsRect(player_rect)) { if (player->velocity.y > 0) { player->position.y -= player->velocity.y; } else { player->position.y = tile.getMaxY(); // if we hit the ground, mark us as grounded so we can jump player->grounded = true; } player->velocity.y = 0; break; } } // unscale the velocity by the inverse delta time and set // the latest position player->position.x = player->position.x + player->velocity.x; player->position.y = player->position.y + player->velocity.y; this->updatePlayerSprite(delta); if(std::find(heldKeys.begin(), heldKeys.end(), RIGHT_ARROW) == heldKeys.end() && player->grounded){ player->velocity.x = 0; player->state = Player::State::Standing; } if(std::find(heldKeys.begin(), heldKeys.end(), LEFT_ARROW) == heldKeys.end() && player->grounded){ player->velocity.x = 0; player->state = Player::State::Standing; } } void GameScene::updatePlayerSprite(float delta){ if(player->state == Player::State::Walking){ if(walkRight->isDone()){ walkRight->startWithTarget(player_sprite); } walkRight->step(delta); if(player->facingRight){ player_sprite->setFlippedX(true); }else { player_sprite->setFlippedX(false); } }else if(player->state == Player::State::Jumping){ player_sprite->setSpriteFrame(Sprite::createWithSpriteFrameName("jump")->getSpriteFrame()); if(player->facingRight){ player_sprite->setFlippedX(true); }else { player_sprite->setFlippedX(false); } }else{ player_sprite->setSpriteFrame(Sprite::createWithSpriteFrameName("idle")->getSpriteFrame()); } player_sprite->setPositionX( player->position.x ); player_sprite->setPositionY( player->position.y ); /*rectWithBorder->setPosition(player_sprite->getBoundingBox().getMinX(), player->position.y);*/ } void GameScene::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event) { if(std::find(heldKeys.begin(), heldKeys.end(), keyCode) == heldKeys.end()){ heldKeys.push_back(keyCode); } } void GameScene::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event) { heldKeys.erase(std::remove(heldKeys.begin(), heldKeys.end(), keyCode), heldKeys.end()); } int GameScene::signum(float x) { if (x > 0.0L) return 1.0L; else if (x < 0.0L) return -1.0L; else return 0.0L; } void GameScene::menuCloseCallback(Ref* pSender) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) return; #endif Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif } GameScene::GameScene(void) { setKeyboardEnabled(true); collidesX = false; } GameScene::~GameScene(void) { } |