//
// HPlayer.cpp
// dafeiji
//
// Created by 丁小未 on 13-9-26.
//
//
#include "HPlayer.h"
#include "HWorld.h"
using namespace cocos2d;
//创建主角精灵
HPlayer *HPlayer::createPlayer(const char* fileName)
{
HPlayer * player=new HPlayer();
//由于继承自CCSprite,然后调用CCSprite的initWithFile方法
if (player && player->initWithFile(fileName)) {
player->autorelease();
//调用初始化方法
player->playerInit();
return player;
}
CC_SAFE_DELETE(player);
return NULL;
}
//设置配置参数,初始化血量,杀敌数等
void HPlayer::playerInit()
{
CCSize size=CCDirector::sharedDirector()->getWinSize();
//设置主角的位置在屏幕下面正中央
this->setPosition(ccp(size.width*0.5, this->getContentSize().height*0.5));
//设置参数(血量,当前血量,无敌时间,分数)
hpMax=5;
hp=5;
score=0;
strongTime=2*60;
//添加右下角的血量贴图
for (int i=0; i<5; i++) {
CCSprite *spHp=CCSprite::create("icon_hp.png");
//设置每个血量贴图在屏幕的右下角一次排列,并且从右至左tag分别是值为5,4,3,2,1的枚举类型
spHp->setPosition(ccp(size.width-this->getContentSize().width*0.5*i-20, spHp->getContentSize().height*0.5));
if (i==0) {
spHp->setTag(tag_playerHP1);
}else if(i==1){
spHp->setTag(tag_playerHP2);
}else if(i==2){
spHp->setTag(tag_playerHP3);
}else if(i==3){
spHp->setTag(tag_playerHP4);
}else if(i==4){
spHp->setTag(tag_playerHP5);
}
//将每一个血量贴图添加到HWorld场景中
HWorld::sharedWorld()->addChild(spHp,10);
}
//添加分数label
CCLabelTTF *label=CCLabelTTF::create("分数", "Helvetica-Blood", 24);
label->setPosition(ccp(30, size.height-22)); //位置左上方
HWorld::sharedWorld()->addChild(label,10);
//分数结果
string strScore=Convert2String(score);
CCLabelTTF * labelScores=CCLabelTTF::create(strScore.c_str(),"Helvetica-Blood",24);
labelScores->setPosition(ccp(110, size.height-22));
labelScores->setColor(ccc3(255, 255, 0)); //设置颜色为黄色
HWorld::sharedWorld()->addChild(labelScores,10,tag_scoreTTF);
//添加杀敌label
CCLabelTTF *labelKill=CCLabelTTF::create("杀敌", "Helvetica-Blood", 24);
labelKill->setPosition(ccp(30, size.height-52));
HWorld::sharedWorld()->addChild(labelKill,10);
//杀敌数
string strKillCount=Convert2String(killCount);
strKillCount+="/100"; //C++中string可以直接拼接
CCLabelTTF *labelKillCount=CCLabelTTF::create(strKillCount.c_str(), "Helvetica-Blood", 24);
labelKillCount->setPosition(ccp(110, size.height-52));
labelKillCount->setColor(ccc3(255, 255, 0)); //设置为黄色
HWorld::sharedWorld()->addChild(labelKillCount,10,tag_killsCountTTF);
}
//添加分数
void HPlayer::addScore(float _value)
{
score+=_value;
string strScore=Convert2String(score);
//根据HWorld获取场景中分数标签并且改变他的值
CCLabelTTF *ttf=(CCLabelTTF *)HWorld::sharedWorld()->getChildByTag(tag_scoreTTF);
ttf->setString(strScore.c_str());
}
//添加杀敌数
void HPlayer::addKillCount(float _value)
{
killCount+=_value;
string strKillCount=Convert2String(killCount);
strKillCount+="/100";
//获取HWorld中杀敌数标签,并且改变他的值
CCLabelTTF *ttf=(CCLabelTTF *)HWorld::sharedWorld()->getChildByTag(tag_killsCountTTF);
ttf->setString(strKillCount.c_str());
//当杀敌过百的时候,游戏胜利
if (killCount>=100) {
//
int oldScore=atoi(CCUserDefault::sharedUserDefault()->getStringForKey("user_score","-1").c_str());//当取出的键值为NULL的时候默认为1
if (oldScore!=-1&&score>oldScore) {
CCUserDefault::sharedUserDefault()->setStringForKey("user_score", Convert2String(score));
CCUserDefault::sharedUserDefault()->flush(); //必须要写,刷新数据
}
//调用胜利界面
HWorld::sharedWorld()->winGame();
}
}
//让主角掉血的方法,分为死亡和飞死亡两种情况来分析
void HPlayer::downHp()
{
//如果还出于无敌状态则不需要掉血
if (isStrong) {
return;
}
//血量减少1
hp-=1;
//如果血量少于0,则将主角设置不可见并且设置他已经死亡
if (hp<=0) {
this->setVisible(false);
isDead=true;
//取出旧的分数
int oldScore=atoi(CCUserDefault::sharedUserDefault()->getStringForKey("user_score","-1").c_str());
//当有新的分数,就将新的分数保存到数据中,并且要刷新保存
if (oldScore!=-1&&score>oldScore)
{
CCUserDefault::sharedUserDefault()->setStringForKey("user_score", Convert2String(score));
CCUserDefault::sharedUserDefault()->flush();
}
HWorld::sharedWorld()->lostGame();
}
//如果主角还为死亡然后移除相应界面中的血量贴图
else
{
switch (hp) {
case 1:
HWorld::sharedWorld()->removeChildByTag(tag_playerHP2, true);
break;
case 2:
HWorld::sharedWorld()->removeChildByTag(tag_playerHP3, true);
break;
case 3:
HWorld::sharedWorld()->removeChildByTag(tag_playerHP4, true);
break;
case 4:
HWorld::sharedWorld()->removeChildByTag(tag_playerHP5, true);
break;
}
//将无敌状态开启,并且开启无敌状态的定时器
isStrong=true;
strongCount=0;//无敌状态的时间
this->schedule(schedule_selector(HPlayer::strongIng));
}
}
//正处于无敌状态中
void HPlayer::strongIng()
{
strongCount++;
//当无敌状态时间到的时候,取消无敌状态,并设置为可见,并且取消定时器
if (strongCount%strongTime==0) {
this->setVisible(true);
isStrong=false;
this->unschedule(schedule_selector(HPlayer::strongIng));
}
else //如果是无敌状态,让他不断的闪烁
{
if (strongCount%3==0) {
this->setVisible(false);
}else{
this->setVisible(true);
}
}
}
//当主角精灵初始化的时候注册他的触摸事件
void HPlayer::onEnter()
{
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
CCSprite::onEnter(); //调用父类的onEnter方法,这里是调用的CCNode的onEnter方法
}
//当主角精灵退出的时候取消他的注册触摸事件
void HPlayer::onExit()
{
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
CCSprite::onExit();
}
//设置主角精灵的当前位置
bool HPlayer::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
this->setPosition(pTouch->getLocation());
return true;
}
//触摸移动的时候不断的去移动主角精灵让他跟随着鼠标移动而移动
void HPlayer::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
this->setPosition(pTouch->getLocation());
}
//触摸结束
void HPlayer::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
this->setPosition(pTouch->getLocation());
}