#include "SGFight.h"
#include "SGCommon.h"
USING_NS_CC;
USING_NS_CC_EXT;
using namespace gui;
const char* FightFontName = "微软雅黑";
void SGFight::onEnter()
{
CCScene::onEnter();
m_pLayer = UILayer::create();
this->addChild(m_pLayer);
ListLength = 0;
BloddListLength = 0;
initUI();
}
void SGFight::onExit()
{
m_pLayer->removeFromParent();
CCScene::onExit();
}
void SGFight::initUI()
{
layoutRoot = static_cast<Layout*>(GUIReader::shareReader()->widgetFromJsonFile("SG_Fight_1/SG_Fight_1.json"));
m_pLayer->addWidget(layoutRoot);
//设置标签字体
Label* labelBag = static_cast<Label*>(UIHelper::seekWidgetByName(layoutRoot, "Label_bag"));
labelBag->setFontName(FightFontName);
Label* labelSoul = static_cast<Label*>(UIHelper::seekWidgetByName(layoutRoot, "Label_soul"));
labelSoul->setFontName(FightFontName);
Label* labelCoin = static_cast<Label*>(UIHelper::seekWidgetByName(layoutRoot, "Label_coin"));
labelCoin->setFontName(FightFontName);
labelCoin->setColor(ccORANGE);
Label* labelRound = static_cast<Label*>(UIHelper::seekWidgetByName(layoutRoot, "Label_round"));
labelRound->setFontName(FightFontName);
//添加按钮响应
Button* buttonSpeend = static_cast<Button*>(layoutRoot->getChildByName("Button_speed"));
buttonSpeend->addTouchEventListener(this, toucheventselector(SGFight::speedCallback));
Button* buttonSkip = static_cast<Button*>(layoutRoot->getChildByName("Button_skip"));
buttonSkip->addTouchEventListener(this, toucheventselector(SGFight::skipCallback));
Button* buttonStart = static_cast<Button*>(layoutRoot->getChildByName("Button_start"));
buttonStart->addTouchEventListener(this, toucheventselector(SGFight::startCallback));
//初始化地图
initMap("map.json", 1);
}
int SGFight::startBattle()
{
arrHero = CCArray::create();
arrEnemy = CCArray::create();
arrHero->retain();
arrEnemy->retain();
for(int i = 1; i < 7; i++)
{
arrHero->addObject((CCObject*)hero[i]->getUserData());
arrEnemy->addObject((CCObject*)enemy[i]->getUserData());
}
int result = 0;
while(1)
{
for(int i = 0; i < 6; i++)
{
if(i < arrHero->count())
{
SGRole* roleHero = (SGRole*)arrHero->objectAtIndex(i);
CCLog("%d %s attack", roleHero->getAttackType(), roleHero->name.getCString());
roleAttack(roleHero, true);
if((result = refreshArr()) > 0)
{
return result;
}
}
if(i < arrEnemy->count())
{
SGRole* roleEnemy = (SGRole*)arrEnemy->objectAtIndex(i);
CCLog("%d %s attack", roleEnemy->getAttackType(), roleEnemy->name.getCString());
roleAttack(roleEnemy, false);
if((result = refreshArr()) > 0)
{
return result;
}
}
}
}
return FIGHT_ENEMY_WIN;
}
void SGFight::roleAttack(SGRole* role, bool isHero)
{
if(isHero)
{
//获得敌方存活人数
int count = arrEnemy->count();
// int target = rand() % count;
int target = CCRANDOM_0_1() * count;
SGRole* targetRole = (SGRole*)arrEnemy->objectAtIndex(target);
int tempHp = targetRole->getCurHp(); //被攻击前的hp
int targetHp = tempHp - role->getAttack() * randHurtPercent();
targetHp = targetHp < 0 ? 0 : targetHp;
targetRole->setCurHp(targetHp);
AnimationList[ListLength++] = role->getAttackType();
AnimationList[ListLength++] = targetRole->getAttackType();
BloodList[BloddListLength++] = targetHp;
CCLog("enemy %d %s being attacked : hp %d -> %d", targetRole->getAttackType() - 6, targetRole->name.getCString(), tempHp, targetHp);
}
else
{
int count = arrHero->count();
// int target = rand() % count;
int target = CCRANDOM_0_1() * count;
SGRole* targetRole = (SGRole*)arrHero->objectAtIndex(target);
int tempHp = targetRole->getCurHp(); //被攻击前的hp
int targetHp = tempHp - role->getAttack() * randHurtPercent();
targetHp = targetHp < 0 ? 0 : targetHp;
targetRole->setCurHp(targetHp);
AnimationList[ListLength++] = role->getAttackType();
AnimationList[ListLength++] = targetRole->getAttackType();
BloodList[BloddListLength++] = targetHp;
CCLog("hero %d %s being attacked : hp %d -> %d", targetRole->getAttackType(), targetRole->name.getCString(), tempHp, targetHp);
}
}
int SGFight::refreshArr()
{
for(int i = arrHero->count() - 1; i >= 0; i--)
{
if(((SGRole*)arrHero->objectAtIndex(i))->getCurHp() <= 0)
{
arrHero->removeObjectAtIndex(i);
if(arrHero->count() == 0)
{
return FIGHT_ENEMY_WIN;
}
}
}
for(int i = arrEnemy->count() - 1; i >= 0; i--)
{
if(((SGRole*)arrEnemy->objectAtIndex(i))->getCurHp() <= 0)
{
arrEnemy->removeObjectAtIndex(i);
if(arrEnemy->count() == 0)
{
return FIGHT_HERO_WIN;
}
}
}
return FIGHT_NORMAL;
}
void SGFight::playAnimation(float dt)
{
static int index = 0;
static int bloodIndex = 0;
int loseHp = 0;
if((index >= ListLength) || (bloodIndex >= BloddListLength))
{
unschedule(schedule_selector(SGFight::playAnimation));
CCLog("schedule end");
return;
}
CCLog("animation 1 begin");
ActionManager::shareManager()->playActionByName("SG_Fight_1.json", CCString::createWithFormat("Animation%d_1", AnimationList[index++])->getCString());
CCLog("refresh blood begin , index = %d", index);
//更新血条
if(AnimationList[index] > 6) //更新敌方血条
{
ImageView* imageHpCur = static_cast<ImageView*>(enemy[AnimationList[index] - 6]->getChildByName("Image_hp_cur"));
if(BloodList[bloodIndex] <= 0)
{
enemy[AnimationList[index] - 6]->setVisible(false);
}
int maxHp = ((SGRole*)enemy[AnimationList[index] - 6]->getUserData())->getHp();
float percentHp = (float)BloodList[bloodIndex] / (float)maxHp;
loseHp = BloodList[bloodIndex] - imageHpCur->getScaleX() * maxHp;
imageHpCur->setScaleX(percentHp);
bloodIndex++;
}
else //跟新我方血条
{
ImageView* imageHpCur = static_cast<ImageView*>(hero[AnimationList[index]]->getChildByName("Image_hp_cur"));
if(BloodList[bloodIndex] <= 0)
{
hero[AnimationList[index]]->setVisible(false);
}
int maxHp = ((SGRole*)hero[AnimationList[index]]->getUserData())->getHp();
float percentHp = (float)BloodList[bloodIndex] / (float)maxHp;
loseHp = BloodList[bloodIndex] - imageHpCur->getScaleX() * maxHp;
imageHpCur->setScaleX(percentHp);
bloodIndex++;
}
CCLog("refresh blood end");
//播放掉血动画
LabelBMFont* labelBlood = static_cast<LabelBMFont*>(layoutRoot->getChildByName(CCString::createWithFormat("Label_hpChange%d", AnimationList[index])->getCString()));
labelBlood->setText(CCString::createWithFormat("%d", loseHp)->getCString());
ActionManager::shareManager()->playActionByName("SG_Fight_1.json", CCString::createWithFormat("Animation%d_3", AnimationList[index])->getCString());
/* for(int i = 7; i < 13; i++)
{
LabelBMFont* labelBlood = static_cast<LabelBMFont*>(layoutRoot->getChildByName(CCString::createWithFormat("Label_hpChange%d", i)->getCString()));
labelBlood->setText(CCString::createWithFormat("%d", loseHp)->getCString());
ActionManager::shareManager()->playActionByName("SG_Fight_1.json", CCString::createWithFormat("Animation%d_3", i)->getCString());
}*/
ActionManager::shareManager()->playActionByName("SG_Fight_1.json", CCString::createWithFormat("Animation%d_2", AnimationList[index++])->getCString());
CCLog("animation 2 end");
}
void SGFight::startCallback(CCObject* pSender, TouchEventType type)
{
if(type == TOUCH_EVENT_ENDED)
{
Button* buttonStart = static_cast<Button*>(layoutRoot->getChildByName("Button_start"));
buttonStart->setPositionX(800);
int result = startBattle();
if(result == FIGHT_ENEMY_WIN)
{
CCLog("enemy win !");
}
else if(result == FIGHT_HERO_WIN)
{
CCLog("hero win !");
}
for(int i = 0; i < ListLength; i += 2)
{
CCLog("%d %d", AnimationList[i], AnimationList[i + 1]);
CCLog("%f", BloodList[i / 2]);
}
CCLog("AnimationListLength = %d, BloodListLength = %d", ListLength, BloddListLength);
sc
- 1
- 2
- 3
- 4
- 5
- 6
前往页