#include "Player.h" USING_NS_CC; Player::Player() : _speed(150), _numBullets(30), _bulletIndex(0), _initialiced(false) { } bool Player::init(){ //primero la superclase if (!Sprite::init()) { return false; } for (int i = 0; i < _numBullets; i++){ _bulletPool.pushBack(Bullet::createPlayerBullet()); } _currentAnimation = IDLE; _controller = TouchController::create(); addChild(_controller); createIdleAnimation(); createExplosionAnimation(); // lanzamos la animacion por defecto runAction(_idleAnimation); scheduleShoot(); return true; } void Player::setParent(Node* parent){ Sprite::setParent(parent); //impide que las balas se incorporen mas de una vez a la escena if (!_initialiced){ for (int i = 0; i < _numBullets; i++){ //incorpora las balas al la escena getParent()->addChild(_bulletPool.at(i)); } _initialiced = true; } } void Player::scheduleShoot(){ //establecemos que la nave disparara cada medio segundo DelayTime *delayAction = DelayTime::create(0.5f); // creamos una accion a partir de una funcion CallFunc *callSelectorAction = CallFunc::create(CC_CALLBACK_0(Player::shoot, this)); //creamos una secuencia que primero espera y luego dispara auto shootSequence = Sequence::create(delayAction, callSelectorAction, NULL); // envolvemos la secuencia un una accion RepeatForever runAction(RepeatForever::create(shootSequence)); } void Player::createIdleAnimation(){ //creamos un pool de imagenes para la animacion VectoranimFrames; auto acc = 0; for (int i = 0; i < 4; i++){ auto frame = SpriteFrame::create("animacion_nave.png", Rect(acc, 0, 50, 63)); acc += 50; animFrames.pushBack(frame); } //si ninguna animacion esta corriendo el esprite por defecto es definido aqui this->setSpriteFrame(animFrames.at(0)); //creamos una animacion con un tiempo de espera entre cada frame de 0.25s auto animation = Animation::createWithSpriteFrames(animFrames, 0.25f); //creamos la accion de animar el objeto animation auto animate = Animate::create(animation); //creamos una accion que repita para siempre la animacion _idleAnimation = RepeatForever::create(animate); // ponemos un tag a la animacion para poder pararla en el futuro _idleAnimation->setTag(Player::Animations::IDLE); } void Player::createExplosionAnimation(){ Vector animFrames; auto acc = 0; for (int i = 0; i < 4; i++){ auto frame = SpriteFrame::create("player_explosion.png", Rect(acc, 0, 50, 63)); acc += 50; animFrames.pushBack(frame); } auto animation = Animation::createWithSpriteFrames(animFrames, 0.15f); //creamos la accion de animar el objeto animation, en este caso no necesitamos //un RepeatForever porque solo debe explotar una vez y parar. _explosionAnimation = Animate::create(animation); _explosionAnimation->setTag(Player::Animations::EXPLOSION); } void Player::update(float dt){ float speed = _speed*dt; auto controller = _controller->getController(); if (controller.upRight){ setPosition(getPositionX() + speed, getPositionY() + speed); } else if (controller.upLeft){ setPosition(getPositionX() - speed, getPositionY() + speed); } else if (controller.downRight){ setPosition(getPositionX() + speed, getPositionY() - speed); } else if (controller.downLeft){ setPosition(getPositionX() - speed, getPositionY() - speed); } else if (controller.left){ setPositionX(getPositionX() - speed); } else if (controller.right){ setPositionX(getPositionX() + speed); } else if (controller.down){ setPositionY(getPositionY() - speed); } else if (controller.up){ setPositionY(getPositionY() + speed); } } void Player::shoot(){ _bulletIndex = _bulletIndex % _numBullets; auto bullet = _bulletPool.at(_bulletIndex); bullet->setAnchorPoint(Point(0.5, 0)); if (!bullet->isVisible()){ bullet->setPosition(getPositionX(),getPositionY()+getBoundingBox().size.height*0.5); bullet->setVisible(true); } _bulletIndex++; }