// Creates an instance of Player // @param png - The path to the PNG representation of the sprite // @param plist - The path to the PLIST representation of the sprite Player* Player::create() { // Create an instance of Player Player* playerSprite = new Player(); // Reset travelled distance playerSprite->setDistanceTravelled(0); // Get resource loader instance ResourceLoader resLoader = ResourceLoader::getInstance(); // Get running animation Vector<SpriteFrame*> runningAnimation = resLoader.getAnimation(PLAYER_ANIMATION_RUNNING); // Generate player movement sprites if (playerSprite->initWithSpriteFrame(runningAnimation.at(0))) { auto animation = Animation::createWithSpriteFrames(runningAnimation); // Set delay between frames animation->setDelayPerUnit(PLAYER_SPRITE_DELAY_RUNNING); //Create running action auto runningAction = RepeatForever::create(Animate::create(animation)); //Set tag for action runningAction->setTag(PLAYER_ANIMATION_RUNNING); // Run animation forever playerSprite->runAction(runningAction); // Set properties playerSprite->autorelease(); playerSprite->initOptions(); playerSprite->setScale(Director::getInstance()->getWinSize().width * 1.8 / 800); playerSprite->scheduleUpdate(); // Add weapon Weapon* weapon = Weapon::create(); playerSprite->setWeapon(weapon); //Create physical body auto spriteBody = PhysicsBody::createBox(playerSprite->boundingBox().size, PhysicsMaterial(1.0f, 0.5f, 0.5f)); spriteBody->setAngularVelocityLimit(0.0f); spriteBody->setMass(1); spriteBody->setCollisionBitmask(PLAYER_COLLISION_BITMASK); spriteBody->setContactTestBitmask(true); playerSprite->setPhysicsBody(spriteBody); // Return return playerSprite; } CC_SAFE_DELETE(playerSprite); return NULL; }
void Player::update(float delta) { CCLOG(to_string(this->getPosition().y).c_str()); distanceTravelled++; framePassed--; if (this->getPhysicsBody()->getVelocity().y > 1000) { this->getPhysicsBody()->setVelocity(Vec2(this->getPhysicsBody()->getVelocity().x, 0)); } if (this->getPosition().y < Director::getInstance()->getWinSize().height / GROUND_PERCENTAGE_FOR_BOX) { this->getPhysicsBody()->setVelocity(Vec2(this->getPhysicsBody()->getVelocity().x, 0)); this->setPosition(Vec2(this->getPosition().x, Director::getInstance()->getWinSize().height / GROUND_PERCENTAGE_FOR_BOX)); } if (this->currentAction != Action::RUNNING && this->getPhysicsBody()->getVelocity().y > -1.0f && this->getPhysicsBody()->getVelocity().y < 1.0f) { // Get resource loader instance ResourceLoader resLoader = ResourceLoader::getInstance(); // Get running animation Vector<SpriteFrame*> runningAnimation = resLoader.getAnimation(PLAYER_ANIMATION_RUNNING); auto animation = Animation::createWithSpriteFrames(runningAnimation); animation->setDelayPerUnit(PLAYER_SPRITE_DELAY_RUNNING); auto runningAction = RepeatForever::create(Animate::create(animation)); runningAction->setTag(PLAYER_ANIMATION_RUNNING); this->runAction(runningAction); this->currentAction = Action::RUNNING; } if ((isKeyPressed(EventKeyboard::KeyCode::KEY_LEFT_ARROW) || isKeyPressed(EventKeyboard::KeyCode::KEY_A)) &&(bossMode)) { this->getPhysicsBody()->applyImpulse(Vect(-((Director::getInstance()->getWinSize().height - 600)/32 + 25), 0)); } if ((isKeyPressed(EventKeyboard::KeyCode::KEY_RIGHT_ARROW) || isKeyPressed(EventKeyboard::KeyCode::KEY_D)) &&(bossMode)) { this->getPhysicsBody()->applyImpulse(Vect(((Director::getInstance()->getWinSize().height - 600) / 32 + 25), 0)); } if (invurnerableTime != 0) invurnerableTime--; if (invurnerable && invurnerableTime == 0) invurnerable = false; }
// Jump action of the Player // Creates an animation , smooth jumping, and possibility of double-jumping if // jump key is pressed during this action. // Sets action to JUMPING void Player::jump() { // Set current action to JUMPING this->currentAction = Action::JUMPING; //Stop running animation and replace the sprite png to the jumping one this->getActionManager()->removeActionByTag(PLAYER_ANIMATION_RUNNING, this); //TODO: Insert the jumping animation instead of a single frame // Get resource loader instance ResourceLoader resLoader = ResourceLoader::getInstance(); // Get running animation Vector<SpriteFrame*> runningAnimation = resLoader.getAnimation(PLAYER_ANIMATION_RUNNING); this->setDisplayFrame(runningAnimation.at(1)); //JUMP //this->getPhysicsBody()->applyImpulse(Vect(0, Director::getInstance()->getWinSize().height *500.0 / 600)); this->getPhysicsBody()->applyImpulse(Vect(0, (Director::getInstance()->getWinSize().height - 600)/3.5 + 500)); }