护盾的实现

在游戏中,由于大多的船的都是长方形的,所以把护盾做成了椭圆,而不是圆,另外也再护盾的外围增加一个护盾的持续时间的一个计时器。效果:


所以,这里当敌人的子弹发射过来时,要在护盾边上触发(也就是椭圆的边上)。

class ProtectBody : public CCSprite {
public:
	static ProtectBody* create(float scaleX, float scaleY);//创造护盾
	bool init(float scaleX, float scaleY);//初始化
	bool checkCollision(const CCPoint &weaponPos);//椭圆碰撞

	void protectStart();//开始启动护盾
	void protectFinished();//护盾启动完成
	void protectEnd();//护盾结束
	void protectTimeEnd();//计时结束

	CC_SYNTHESIZE_READONLY(float, _sx, SX);//x轴缩放(不同的体型控制不同的缩放)
	CC_SYNTHESIZE_READONLY(float, _sy, SY);//y轴缩放(不同的体型控制不同的缩放)
	CC_SYNTHESIZE_READONLY(bool, _active, Active);//是否激活

private:
	CCProgressTimer* _protectTime;//计时器
};
实现的代码十分简单,可以一目了然。
ProtectBody* ProtectBody::create(float scaleX, float scaleY) {
	ProtectBody* protectBody = new ProtectBody();
	if(protectBody && protectBody->init(scaleX, scaleY)) {
		protectBody->autorelease();
		return protectBody;
	}else {
		delete protectBody;
		protectBody = NULL;
		return NULL;
	}
}
bool ProtectBody::init(float scaleX, float scaleY) {
	bool result = false;
	if(CCSprite::init()) {//初始化
		this->_active = false;
		this->_sx = scaleX;
		this->_sy = scaleY;
		this->initWithSpriteFrameName(protect_body_png);
		this->setScale(0);
		this->setOpacity(0);
		ccBlendFunc blend = {GL_SRC_ALPHA, GL_ONE};
		this->setBlendFunc(blend);

		this->_protectTime = CCProgressTimer::create(CCSprite::createWithSpriteFrameName(protect_time_png));
		this->_protectTime->setOpacity(50);
		this->_protectTime->setVisible(false);
		this->_protectTime->setType(kCCProgressTimerTypeRadial);
		this->_protectTime->setReverseProgress(true);
		CCSize size = this->getContentSize();
		this->_protectTime->setPosition(ccp(size.width / 2, size.height / 2));
		this->addChild(this->_protectTime);

		result = true;
	}
	return result;
}
bool ProtectBody::checkCollision(const CCPoint &weaponPos) {
	CCPoint shipPos = GameLayer::shareGameLayer()->getShip()->getPosition();
	CCSize s = this->getContentSize();
	//椭圆碰撞
	float x = weaponPos.x - shipPos.x;
	float y = weaponPos.y - shipPos.y;
	float w = s.width * this->_sx;
	float h = s.height * this->_sy;
	float r = (x * x) / (w * w) + (y * y) / (h * h);
	if(abs(r) <= 0.25) {
		return true;
	}
	return false;
}
void ProtectBody::protectStart() {
	this->_active = true;
	CCActionInterval* scaleAction = CCScaleTo::create(0.5f, this->_sx, this->_sy);
	CCActionInterval* fadeAction = CCFadeTo::create(0.5f, 255);
	CCCallFunc* callback = CCCallFunc::create(this, callfunc_selector(ProtectBody::protectFinished));
	this->runAction(CCSequence::create(CCSpawn::create(scaleAction, fadeAction, NULL), callback, NULL));
	
}
void ProtectBody::protectFinished() {
	GameLayer::shareGameLayer()->getShip()->setIsProtected(true);
	CCProgressFromTo* to = CCProgressFromTo::create(STATIC_DATA_FLOAT("protect_time"), 100, 0);
	CCCallFunc* callback = CCCallFunc::create(this, callfunc_selector(ProtectBody::protectEnd));
	this->_protectTime->setPercentage(100);
	this->_protectTime->setVisible(true);
	this->_protectTime->runAction(CCSequence::create(to, callback, NULL));
}
void ProtectBody::protectEnd() {
	CCActionInterval* scaleAction = CCScaleTo::create(0.5f, 0, 0);
	CCActionInterval* fadeAction = CCFadeTo::create(0.5f, 0);
	CCCallFunc* callback = CCCallFunc::create(this, callfunc_selector(ProtectBody::protectTimeEnd));
	this->runAction(CCSequence::create(CCSpawn::create(scaleAction, fadeAction, NULL), callback, NULL));
	GameLayer::shareGameLayer()->getShip()->setIsProtected(false);
	GameLayer::shareGameLayer()->setIsAppearShield(false);
}
void ProtectBody::protectTimeEnd() {
	this->_active = false;//不激活
	this->_protectTime->setVisible(false);//计时器设置不可见
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个经典的贪心算法问题,可以使用贪心算法来解决。下面是一个简单的 Python 代码实现: ```python # 设备列表,每个元素为一个二元组 (设备编号, 护盾防御值) devices = [(1, 10), (2, 5), (3, 7), (4, 8), (5, 6)] # 按照护盾防御值从大到小排序 devices.sort(key=lambda x: x[1], reverse=True) # 初始化已选设备列表和总防御值 selected_devices = [] total_defense = 0 # 依次选择护盾防御值最高的设备,如果该设备的编号与已选设备列表中的任何设备编号重复,则跳过该设备 for device in devices: if all(device[0] != selected_device[0] for selected_device in selected_devices): selected_devices.append(device) total_defense += device[1] # 输出最终选择的设备和总防御值 print("选择的设备:", selected_devices) print("总防御值:", total_defense) ``` 输出结果如下: ``` 选择的设备: [(1, 10), (4, 8), (3, 7)] 总防御值: 25 ``` 在这个示例中,我们有 5 个设备,每个设备具有一个护盾防御值。我们需要选择一些设备来保护,同时最小化使用的护盾数量。通过按照护盾防御值从大到小对设备列表进行排序,我们可以先选择护盾防御值最高的设备,然后逐个检查其他设备是否与已选设备列表中的任何设备编号重复。如果没有,我们将其添加到已选设备列表中,并增加总防御值。最终,我们选择了 3 个设备,并用了总共 25 点护盾来保护它们。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值