您的位置:首页 > 其它

主要内容

2014-09-14 23:34 211 查看
//

// GameScene.cpp

// HelloCpp

//

// Created by liufuyu on 14-9-11.

//

//

#include "GameScene.h"

Vector<Enemy*>GameScene::allEnemy;

Vector<Bullet *>GameScene::allBullet;

Scene* GameScene::createScene()

{

auto scene=Scene::create();

auto layer=GameScene::create();

scene->addChild(layer);

return scene;

}

bool GameScene::init()

{

if(!Layer::init())

{

return false;

}

//加载plist文件到SpriteFrameCatch

SpriteFrameCache::getInstance()->addSpriteFramesWithFile("Play.plist");

auto bk=Sprite::create("playbg.png");

bk->setPosition(Director::getInstance()->getWinSize()/2);

this->addChild(bk);

auto map=TMXTiledMap::create("map.tmx");

map->setTag(1000);

this->addChild(map);

//map->setAnchorPoint(Vec2(0.5,0.5));忘记加Vec2

//map->setPosition(Vec2(Director::getInstance()->getWinSize()/2));

initAllPoint(map);

this->schedule(schedule_selector(GameScene::newEnemy),3);//一秒产生一个怪物

//接受用户事件触摸

auto listener=EventListenerTouchOneByOne::create();

listener->onTouchBegan=CC_CALLBACK_2( GameScene::onTouchBegan, this);

listener->onTouchMoved=CC_CALLBACK_2( GameScene::onTouchMoved, this);

listener->onTouchEnded=CC_CALLBACK_2( GameScene::onTouchEnded, this);

listener->setSwallowTouches(true);//

Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);

this->scheduleUpdate();

return true;

}

//加载对象层的所有点

void GameScene::initAllPoint(TMXTiledMap*map)

{

auto objs=map->getObjectGroup("obj");

for(int i=0;i<objs->getObjects().size();i++)

{ //得到每一个点

Mypoint* tmp=Mypoint::create();

tmp->x=objs->getObject(StringUtils::format("%d",i))["x"].asFloat();

tmp->y=objs->getObject(StringUtils::format("%d",i))["y"].asFloat();

this->allPoint.pushBack(tmp);

}

}

void GameScene::newEnemy(float t)

{

int type=random()%3+1;

auto newEnemy=Enemy::createEnemy(type,allPoint);

this->addChild(newEnemy);//添加到当前图层

allEnemy.pushBack(newEnemy);

}

bool GameScene::onTouchBegan(Touch *touch, Event *unused_event)

{

int r=touch->getLocationInView().y/96;

int c=touch->getLocationInView().x/96;

auto map=(TMXTiledMap*)this->getChildByTag(1000);

int gid=map->getLayer("bg")->getTileGIDAt(Vec2(c,r));

CCLOG("您点中了第%d行,第%d列 这个块的编号是%d",r,c,gid);

bool canTouch=false;//默认不可以放塔

if(gid>0)

{

if(!map->getPropertiesForGID(gid).isNull())

{

auto tileMemp =map->getPropertiesForGID(gid).asValueMap();

//判断编号为gid的tiled有没有属性

if(!tileMemp.empty())

{ canTouch=true;

CCLOG("这里可以放塔gid cantTouch=%d",tileMemp.at("canTouch").asInt());

}

}

}

nowRow=((int)(touch->getLocation().y/96));

nowCol=((int)(touch->getLocation().y/96));

if(mapinfo[nowRow][nowCol]==0)

{

if(canTouch)

{

//移除旧的塔的选择

if(this->getChildByTag(1001)!=nullptr)

{

this->getChildByTag(1001)->removeFromParentAndCleanup(true);

}

//添加塔的选择图层

addTDselect(r,c);//添加塔的选择图层

}else

{

auto tips=Sprite::createWithSpriteFrameName("no.png");

tips->setAnchorPoint(Vec2(0,1));

tips->setPosition(c*96,(9-r)*96);

this->addChild(tips);

tips->runAction(Sequence::create(DelayTime::create(0.8f),

CallFunc::create(CC_CALLBACK_0(Sprite::removeFromParent, tips)) ,NULL ));

}

// else

// {//出现塔升级或者卖掉的图层

// }

return true;

}

else{}

}

void GameScene::onTouchMoved(Touch *touch, Event *unused_event)

{

}

void GameScene::onTouchEnded(Touch *touch, Event *unused_event)

{

}

void GameScene::addTDselect(int r,int c)

{ this->nowCol=c;

this->nowRow=r;

auto Image=Sprite::createWithSpriteFrameName("towerPos.png");

int height=Image->getContentSize().height;

int width=Image->getContentSize().width;

auto bt01=Sprite::createWithSpriteFrameName("ArrowTower1.png");

auto bt01_select=Sprite::createWithSpriteFrameName("ArrowTower1.png");

bt01_select->setScale(1.1);

auto bt02=Sprite::createWithSpriteFrameName("AttackTower1.png");

auto bt02_select=Sprite::createWithSpriteFrameName("AttackTower1.png");

bt02_select->setScale(1.1);

auto bt03=Sprite::createWithSpriteFrameName("multiDirTower.png");

auto bt03_select=Sprite::createWithSpriteFrameName("multiDirTower.png");

bt03_select->setScale(1.1);

//将3个Sprite转为menu 接受用户事件

auto mitem01=MenuItemSprite::create(bt01,bt01_select,CC_CALLBACK_1(GameScene::selectTD, this));

auto mitem02=MenuItemSprite::create(bt02,bt02_select,CC_CALLBACK_1(GameScene::selectTD, this));

auto mitem03=MenuItemSprite::create(bt03,bt03_select,CC_CALLBACK_1(GameScene::selectTD, this));

mitem01->setTag(10);

mitem02->setTag(11);

mitem03->setTag(12);

mitem01->setAnchorPoint(Vec2(1,0));

mitem02->setAnchorPoint(Vec2(0.5,0));

mitem03->setAnchorPoint(Vec2(0,0));

auto menuTD=Menu::create( mitem01, mitem02, mitem03,nullptr);

Image->addChild(menuTD);

menuTD->setPosition(Vec2::ZERO);

// Image->addChild(bt01);

// Image->addChild(bt02);

// Image->addChild(bt03);

Image->setTag(1001);

mitem01->setPosition(Vec2(0,height));

mitem02->setPosition(Vec2(width/2,height));

mitem03->setPosition(Vec2(width,height));

this->addChild(Image);

Image->setAnchorPoint(Vec2(0,1));

Image->setPosition(c*96,(9-r)*96);

// Image->runAction(Sequence::create(DelayTime::create(5.0f),

// CallFunc::create(CC_CALLBACK_0(Sprite::removeFromParent, Image)) ,NULL ));

}

void GameScene::selectTD(Ref * obj)

{

auto item=(MenuItemSprite*)obj;

switch(item->getTag())

{

case 10://塔1

{ //创建一个新塔

TD *newTd=TD::createNewTD(1, nowRow, nowCol);

this->addChild(newTd);

//设置地图记录

allTD.pushBack(newTd);

mapinfo[nowRow][nowCol]=allTD.getIndex(newTd)+1;//在地图中做记录

}

break;

case 11://塔2

break;

case 12:

break;

}

CCLOG("您选中了%d",item->getTag());

//移除旧的塔的选择

if(this->getChildByTag(1001)!=nullptr)

{

this->getChildByTag(1001)->removeFromParentAndCleanup(true);

}

}

void GameScene::update(float t)

{

for(int i=0;i<GameScene::allBullet.size();i++)

{

Bullet *nowBullet=allBullet.at(i);//得到第i个子弹

for(int j=0;j<GameScene::allEnemy.size();j++)

{

Enemy *nowEnemy=GameScene::allEnemy.at(j);//得到第j个子弹

Rect rb(

nowBullet->px,

nowBullet->py,

30,

30

);

Rect re(nowEnemy->getPosition().x,

nowEnemy->getPosition().y,

100,

100

);

if(rb.intersectsRect(re))

{//怪物掉血

nowEnemy->hp--;

nowEnemy->sHp();

if(nowEnemy->hp<=0)

{

//添加爆炸效果

auto exp=Explode::newExplode(1, nowEnemy->getPosition().x,nowEnemy->getPosition().y);

this->addChild(exp);

//移除怪物

nowEnemy->unscheduleAllSelectors();

nowEnemy->stopAllActions();

nowEnemy->removeFromParentAndCleanup(true);

GameScene::allEnemy.erase(j);

}

nowBullet->unscheduleAllSelectors();

nowBullet->stopAllActions();

nowBullet->removeFromParentAndCleanup(true);

GameScene::allBullet.erase(i);

i--;

break;

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: