塔防游戏的几个基本要点分析如下:
第一:小怪的行进路线,通过waypoint来实现,waypoint是一个链表,小怪从一个位置开始,判断当前位置和目的位置,通过圆形碰撞,如果到了目的位置,就从链表中取下个位置。如果到达最后位置,即链表最后节点,玩家血量减一。
if (theGame->collisionWithCircle(myPostion,1, destnationWaypoint->myPostion,1))//到了一个路径点,就走下一个路径点
{
if (destnationWaypoint->nextWayPoint) {
destnationWaypoint=destnationWaypoint->nextWayPoint;
}
else
{
//玩家掉血
theGame->getHpDamage();
removeSelf();
}
}
CCPoint targetPoint=destnationWaypoint->myPostion;
float movementSpeed=walkingSpeed;
CCPoint normalized=ccpNormalize(ccp(targetPoint.x-myPostion.x,targetPoint.y-myPostion.y));
double angle=atan2(normalized.y, -normalized.x);
float rotation=CC_RADIANS_TO_DEGREES(angle); //旋转方向
mySprite->setRotation(rotation);
myPostion=ccp(myPostion.x+normalized.x*movementSpeed,myPostion.y+normalized.y*movementSpeed);//移动距离
mySprite->setPosition(myPostion);
第二:小怪会被炮塔攻击,也就是小怪进了炮塔攻击范围。也是一个圆形碰撞。小怪离开后,炮塔不再攻击。
第三:炮塔的位置可以写在文件中,当用户手指碰到屏幕时。计算碰撞,是否在位置范围内,是则添加炮塔。
第四:怪物是一波一波的,当所有怪物消失后,才载入文件,放入下波怪。
源码地址:http://download.csdn.net/detail/cloud95/5272112