Created
April 27, 2018 20:48
-
-
Save 5HARK/18c1a08cf078b4eb5bb81d3c4e4421b5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // Player.cpp | |
| // NewCocosProject1 | |
| // | |
| // Created by jeunkim on 2017. 9. 13.. | |
| // | |
| // | |
| #include "Player.h" | |
| Player* Player::instance = nullptr; | |
| Player :: Player() | |
| { | |
| } | |
| Player :: ~Player() | |
| { | |
| this->removeAllChildren(); | |
| leftRun->release(); | |
| rightRun->release(); | |
| leftAvoid->release(); | |
| rightAvoid->release(); | |
| leftAimAvoid->release(); | |
| rightAimAvoid->release(); | |
| } | |
| Player* Player::getInstance() | |
| { | |
| if(instance == nullptr) | |
| { | |
| instance = new Player; | |
| } | |
| return instance; | |
| } | |
| void Player:: purgeInstance() | |
| { | |
| if(instance != nullptr) | |
| { | |
| delete instance; | |
| instance = nullptr; | |
| } | |
| } | |
| PlayerState Player:: GetState() | |
| { | |
| return state; | |
| } | |
| void Player::StopAvoidAni() | |
| { | |
| this->stopAllActions(); | |
| aim->stopAllActions(); | |
| this->SetDirection(); | |
| } | |
| void Player :: StopRunAni() | |
| { | |
| this->stopAllActions(); | |
| this->SetDirection(); | |
| } | |
| void Player :: StartRunAni(PlayerState _state) | |
| { | |
| if(state == _state) return ; | |
| if(state == PlayerState::AIM || state == PlayerState::AVOID) return ; | |
| if(_state == PlayerState::LEFT) | |
| { | |
| this->stopAllActions(); | |
| this->runAction(leftRun); | |
| } | |
| else if(_state == PlayerState::RIGHT ) | |
| { | |
| this->stopAllActions(); | |
| this->runAction(rightRun); | |
| } | |
| else { | |
| this->StopRunAni(); | |
| } | |
| this->SetState(_state); | |
| } | |
| void Player::SetState(PlayerState _state) | |
| { | |
| state = _state; | |
| } | |
| void Player :: Avoid() | |
| { | |
| } | |
| void Player ::update(float dt) | |
| { | |
| } | |
| void Player:: SetAnimation() | |
| { | |
| auto leftRunAni = Animation::create(); | |
| for(int i = 1; i <=9; i++) | |
| { | |
| string filename = StringUtils::format("player/run/rn_%d.png",i); | |
| leftRunAni->addSpriteFrameWithFile(filename); | |
| } | |
| leftRunAni->setDelayPerUnit(0.1f); | |
| leftRunAni->setRestoreOriginalFrame(true); | |
| auto anim = Animate::create(leftRunAni); | |
| auto rev = anim->reverse(); | |
| auto leftRunSeq = Sequence :: create(anim,rev, nullptr); | |
| leftRun = RepeatForever::create(leftRunSeq); | |
| leftRun->retain(); | |
| auto rightRunAni = Animation::create(); | |
| for(int i = 10; i <=18; i++) | |
| { | |
| string filename = StringUtils::format("player/run/rn_%d.png",i); | |
| rightRunAni->addSpriteFrameWithFile(filename); | |
| } | |
| rightRunAni->setDelayPerUnit(0.1f); | |
| rightRunAni->setRestoreOriginalFrame(true); | |
| anim = Animate::create(rightRunAni); | |
| rev = anim->reverse(); | |
| auto rightRunSeq = Sequence :: create(anim,rev, nullptr); | |
| rightRun = RepeatForever::create(rightRunSeq); | |
| rightRun->retain(); | |
| auto avoidAni = Animation::create(); | |
| avoidAni->setDelayPerUnit(0.1); | |
| for(int i = 1; i<=3; i++) | |
| { | |
| string filename = StringUtils::format("player/avoid/av_%d.png",i); | |
| avoidAni->addSpriteFrameWithFile(filename); | |
| } | |
| anim = Animate::create(avoidAni); | |
| auto repeat = Repeat::create(anim, 1); | |
| rightAimAvoid = AvoidAction::create(0.2, Vec2(300,0)); | |
| leftAimAvoid = AvoidAction::create(0.2, Vec2(-300,0)); | |
| leftAimAvoid->retain(); | |
| rightAimAvoid->retain(); | |
| auto func = CallFunc::create([&](){ | |
| this->StopAvoidAni(); | |
| this->SetState(PlayerState::WAIT); | |
| }); | |
| auto spawn = Spawn::create(repeat,leftAimAvoid,nullptr); | |
| leftAvoid = Sequence::create(spawn,func,nullptr); | |
| leftAvoid->retain(); | |
| spawn = Spawn::create(repeat,rightAimAvoid,nullptr); | |
| rightAvoid = Sequence::create(spawn,func,nullptr); | |
| rightAvoid->retain(); | |
| } | |
| void Player::SetInitPostion() | |
| { | |
| playerSize = this->getContentSize(); | |
| this->setPosition(Point(visibleSize.width /2, playerSize.height / 2 + 100.0f)); | |
| playerPos = this->getPosition(); | |
| aim->setPosition(playerPos.x , playerPos.y + 300.0f ); | |
| aimPos = aim->getPosition(); | |
| aimSize = aim->getContentSize(); | |
| } | |
| bool Player :: initWithFile(const std::string &fileName) | |
| { | |
| if(!Sprite::initWithFile(fileName)) | |
| { | |
| return false; | |
| } | |
| currentPlayer = "player/shoot/sh_3.png"; | |
| this->SetAnimation(); | |
| aim = GunAim::create("ui/target.png"); | |
| this->SetInitPostion(); | |
| return true; | |
| } | |
| void Player::StartAvoidAni() | |
| { | |
| log("A"); | |
| if(state == PlayerState::LEFT) | |
| { | |
| this->runAction(leftAvoid); | |
| aim->runAction(leftAimAvoid); | |
| } | |
| else if(state == PlayerState::RIGHT) | |
| { | |
| this->runAction(rightAvoid); | |
| aim->runAction(rightAimAvoid); | |
| } | |
| else if(state == PlayerState::WAIT){ | |
| float aimX = aim->getPositionX(); | |
| if(aimX >= this->getPositionX()) | |
| { | |
| this->runAction(rightAvoid); | |
| aim->runAction(rightAimAvoid); | |
| } | |
| else | |
| { | |
| this->runAction(leftAvoid); | |
| aim->runAction(leftAimAvoid); | |
| } | |
| } | |
| else { | |
| this->SetState(PlayerState::WAIT); | |
| return ; | |
| } | |
| this->SetState(PlayerState::AVOID); | |
| } | |
| void Player::SetDirection() | |
| { | |
| float dY = (aim->getPositionY() - this->getPositionY()); | |
| float dX = (aim->getPositionX() - this->getPositionX()); | |
| float angle = atan2f(dY, dX); | |
| float deg = angle * 180.0f / M_PI; | |
| deg = abs(deg); | |
| if( .0f <= deg && deg <= 36.0f ) | |
| { | |
| currentPlayer = "player/shoot/sh_4.png"; | |
| } | |
| else if(36.0f < deg && deg <= 72.0f) | |
| { | |
| currentPlayer = "player/shoot/sh_5.png"; | |
| } | |
| else if (72.0f < deg && deg <= 108.0f) | |
| { | |
| currentPlayer = "player/shoot/sh_3.png"; | |
| } | |
| else if(108.0f < deg && deg <= 144.0f) | |
| { | |
| currentPlayer = "player/shoot/sh_1.png"; | |
| } | |
| else { | |
| currentPlayer = "player/shoot/sh_2.png"; | |
| } | |
| this->setTexture( Director::getInstance()->getTextureCache()->addImage(currentPlayer.c_str()) ); | |
| } | |
| void Player::CheckAndMoveAim(float moveX, float moveY) | |
| { | |
| aimPos = aim->getPosition(); | |
| if (aimSize.width / 2 < aimPos.x + moveX * _speed && aimPos.x + moveX * _speed < visibleSize.width - aimSize.width / 2) | |
| { | |
| aimPos.x += moveX * _speed ; | |
| } | |
| if (playerPos.y - playerSize.height / 2 + aimSize.height / 2 < aimPos.y + moveY * _speed && aimPos.y + moveY * _speed < visibleSize.height - aimSize.height / 2) | |
| { | |
| aimPos.y += moveY * _speed ; | |
| } | |
| aim->setPosition(aimPos); | |
| } | |
| void Player::CheckAndMovePlayer(float moveX, float moveY) | |
| { | |
| playerPos = this->getPosition(); | |
| aimPos = aim->getPosition(); | |
| if (playerSize.width /2 < playerPos.x + moveX * _speed && playerPos.x + moveX * _speed < visibleSize.width - playerSize.width / 2) | |
| { | |
| playerPos.x += moveX * _speed ; | |
| aimPos.x += moveX * _speed ; | |
| if (aimSize.width /2 >= aimPos.x + moveX * _speed || aimPos.x + moveX * _speed >= visibleSize.width - aimSize.width / 2) | |
| { | |
| aimPos.x -= moveX * _speed ; | |
| } | |
| } | |
| this->setPosition(playerPos); | |
| aim->setPosition(aimPos); | |
| } | |
| void Player::updatePlayerCoin(){ | |
| cocos2d::network::HttpClient* client = cocos2d::network::HttpClient::getInstance(); | |
| client->setTimeoutForConnect(3000); | |
| client->setTimeoutForRead(3000); | |
| __String *dataToSend = __String::create( "userid=1" ); | |
| cocos2d::network::HttpRequest* request = new cocos2d::network::HttpRequest(); | |
| request->setUrl("http://192.168.0.200:8080/money_select.php"); | |
| request->setRequestType(cocos2d::network::HttpRequest::Type::POST); | |
| request->setRequestData(dataToSend->getCString(), dataToSend->length()); | |
| request->setResponseCallback(std::bind(&Player::onHttpRequestCompleted, this, std::placeholders::_1, std::placeholders::_2)); | |
| cocos2d::network::HttpClient::getInstance()->send(request); | |
| } | |
| void Player::onHttpRequestCompleted(cocos2d::network::HttpClient *sender, cocos2d::network::HttpResponse *response){ | |
| if(200 == response->getResponseCode()){ | |
| std::vector<char> *buffer = response->getResponseData(); | |
| std::string asd(buffer->begin(), buffer->end()); | |
| stringstream ss(asd); | |
| int myNr; | |
| if((ss >> myNr).fail()){ | |
| this->coin = -1; | |
| }else{ | |
| this->coin = myNr; | |
| } | |
| }else{ | |
| this->coin = -1; | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment