#include "Game.h"
#include "SimpleAudioEngine.h"
#pragma execution_character_set("utf-8")
#include <math.h>
#include <cmath>
#include "Player.h"
#include <string>
#include "GameOver.h"
using namespace std;
USING_NS_CC;
Scene* CGame::createScene()
{
Scene* scene = Scene::create();
CGame* gameLayer = CGame::create();
scene->addChild(gameLayer);
return scene;
}
bool CGame::init()
{
if (!Layer::init())
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();//可见区的size
Vec2 origin = Director::getInstance()->getVisibleOrigin();//窗口的原点
SpriteFrameCache* pCacha = SpriteFrameCache::getInstance();
pCacha->addSpriteFramesWithFile("Jump/box.plist");
//背景
Sprite* pGame = Sprite::create("game.jpg");
pGame->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2));
this->addChild(pGame);
//箱子管理者
m_pBoxMgr = CBoxMgr::create();
this->addChild(m_pBoxMgr);
//m_pCurBox = m_pBoxMgr->getCurBox();//实施刷新下一个盒子
//玩家
m_pPlayer = CPlayer::create();
m_pPlayer->setAnchorPoint(Vec2(0.5, 0));
m_pPlayer->setPosition(Vec2(origin.x, origin.y - 40));
//把玩家挂在节点上
m_pPlayerNode = Node::create();
m_pPlayerNode->setScale(0.5, 0.5);
m_pPlayerNode->setPosition(Vec2(origin.x + 150, origin.y + 150));
m_pPlayerNode->addChild(m_pPlayer);
this->addChild(m_pPlayerNode, 1);
EventListenerMouse* _mouListenerMouse = EventListenerMouse::create();
_mouListenerMouse->onMouseDown = CC_CALLBACK_1(CGame::onMouseDown, this);
_mouListenerMouse->onMouseUp = CC_CALLBACK_1(CGame::onMouseUp, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(_mouListenerMouse, m_pPlayerNode);
this->scheduleUpdate();//开启定时器让update计算按下的时间,计不计算看鼠标的状态
return true;
}
CGame::CGame()
{
m_fDownTime = 0;
m_fPlayerJumpDistrance = 0;
m_fJumpPower = 75;
m_fJumpSpeed = 0;
m_bPlayAction = false;
m_bMouseDown = false;
m_bDie = false;
m_bAdd = false;
}
CGame::~CGame()
{
}
void CGame::onMouseDown(Event* event)
{
// EventMouse* e = (EventMouse*)event;
// float mouseDownX = e->getCursorX();
// float mouseDownY = e->getCursorY();
// CCLOG("鼠标按下!%f %f",mouseDownX,mouseDownY);
//this->scheduleUpdate();
m_bMouseDown = true;
}
void CGame::onMouseUp(Event* event)
{
if (!m_bPlayAction)
{
m_bPlayAction = true;
//this->unscheduleUpdate();//停止定时器结算按下鼠标的时间
//CCLOG("鼠标松开!");
//CCLOG("鼠标按下的时间:%f", m_fDownTime);
m_fJumpSpeed += m_fJumpPower*m_fDownTime;
m_fPlayerJumpDistrance = m_fJumpSpeed*m_fDownTime;
float fBoxX = m_fPlayerJumpDistrance*cos(3.14 / 6);//盒子的位置偏移
float fBoxY = m_fPlayerJumpDistrance*sin(3.14 / 6);
Vec2 player = m_pPlayerNode->getPosition();
Vec2 NextBox = m_pNextBox->getPosition();
Vec2 newNextBox = m_pBoxMgr->convertToWorldSpace(NextBox);
float fDistrancePB = sqrt((newNextBox.x - player.x)*(newNextBox.x - player.x) + (newNextBox.y - player.y)*(newNextBox.y - player.y));//计算玩家和下一个盒子的距离
float fX = (newNextBox.x - player.x) / fDistrancePB*m_fPlayerJumpDistrance;//玩家位置的偏移
float fY = (newNextBox.y - player.y) / fDistrancePB*m_fPlayerJumpDistrance;
ScaleTo* scaleAtc = ScaleTo::create(0.3f, 1);
JumpBy* jumpAct = JumpBy::create(0.3f, Vec2(fX, fY), 50, 1);
RotateBy* rotateAct = RotateBy::create(0.3f, 360);
Spawn* spawnAct = Spawn::create(jumpAct, rotateAct, NULL);//绑定序列动作同步执行
m_pPlayerNode->runAction(spawnAct);
m_pPlayer->runAction(scaleAtc);
m_fJumpSpeed = 0;
m_fDownTime = 0;
}
}
void CGame::update(float delta)
{
Rect playerRect = m_pPlayer->getBoundingBox();//得到玩家的包围盒
Vec2 newOriginPlayer = m_pPlayerNode->convertToWorldSpace(playerRect.origin);//把玩家的包围盒起点转化为世界坐标
m_rPlayerBox = Rect(newOriginPlayer, playerRect.size*0.8);//画出一个新的包围盒
m_pNextBox = m_pBoxMgr->getNextBox();
Rect nextBoxRect = m_pNextBox->getBoundingBox();//得到下一个Box的包围盒
Vec2 newNextOriginBox = m_pBoxMgr->convertToWorldSpace(nextBoxRect.origin);
Rect newNextBoxRect = Rect(newNextOriginBox, nextBoxRect.size);
m_rNextBox = Rect(newNextBoxRect.getMinX() + 0.52*newNextBoxRect.size.width, newNextBoxRect.getMinY() +
0.5*newNextBoxRect.size.height, 0.27*newNextBoxRect.size.width, 0.43*newNextBoxRect.size.height);//画一个新的包围盒
m_pCurBox = m_pBoxMgr->getCurBox();
Rect curBoxRect = m_pCurBox->getBoundingBox();//得到下一个Box的包围盒
Vec2 newCurOriginBox = m_pBoxMgr->convertToWorldSpace(curBoxRect.origin);
Rect newCurBoxRect = Rect(newCurOriginBox, curBoxRect.size);
m_rCurBox = Rect(newCurBoxRect.getMinX() + 0.52*newCurBoxRect.size.width, newCurBoxRect.getMinY() +
0.5*newCurBoxRect.size.height, 0.27*newCurBoxRect.size.width, 0.43*newCurBoxRect.size.height);//画一个新的包围盒
if (!m_bPlayAction&&m_bMouseDown)//鼠标按下的时候,玩家的蓄力情况
{
m_fDownTime += delta;
float fScale = m_pPlayer->getScaleY();//压缩
fScale -= 0.01*m_fDownTime;
if (fScale <= 0.5)
{
fScale = 0.5;
}
m_pPlayer->setScaleY(fScale);
}
else if (m_bPlayAction)
{
m_fDownTime += delta;
if (m_fDownTime>=0.3f)
{
m_bPlayAction = false;
m_bMouseDown = false;
m_fDownTime = 0;
}
}
if (!m_bMouseDown&&m_rPlayerBox.intersectsRect(m_rNextBox))
{
m_bAdd = false;
CCLOG("相交");
float fIntersectX = m_rPlayerBox.getMaxX() - m_rNextBox.getMinX();//相交部分的Width
float fIntersectY = m_rNextBox.getMaxY() - m_rPlayerBox.getMinY();//相交部分的Width
float fScaleX = fIntersectX / m_rPlayerBox.size.width;
float fScaleY = fIntersectY / m_rPlayerBox.size.height;
if (0.45<=fScaleX&&0.13<=fScaleY)
{
m_pBoxMgr->setNextBox();
if (m_pBoxMgr->getAddState()==E_ADD_NOFIRST)
{
Vec2 vMoveBy = m_pBoxMgr->getVecMove();
MoveBy* move = MoveBy::create(0.5f, Vec2(vMoveBy));
m_pPlayerNode->runAction(move);
m_bAdd = true;
}
else if (m_pBoxMgr->getAddState() == E_ADD_FIRST)
{
Vec2 vMoveBy = m_pBoxMgr->getVecMove();
MoveBy* move = MoveBy::create(0.5f, Vec2(0,vMoveBy.y));
m_pPlayerNode->runAction(move);
m_bAdd = true;
}
}
if (!m_bAdd)
{
this->scheduleOnce(CC_CALLBACK_1(CGame::changeScene,this), 0.0f,"changeScene");
this->unscheduleUpdate();
}
}
}
void CGame::changeScene(float)
{
Scene* pOverScene = CGameOver::createScene();
Director::getInstance()->replaceScene(TransitionSlideInT::create(1.0f, pOverScene));
}