Beispiel #1
0
//尝试给对象添加一个效果
func (self *EffectBase) PutOn(from interface{}, target effect.EffectCarrier) bool {
	//如果效果还没有对象产生
	if self.Target == nil {

		//判断接收对象是否可以放置本效果
		isTargetReceive := target.PutOnEffect(self.Holder, from)

		if isTargetReceive {
			self.PutOnTime = time.GetNow()
			self.From = from
			self.Target = target
			self.Alive = true
			return true
		} else {
			//效果不被对方接收(比如:可能对方在效果 OnBeforePutOn 事件中,被其他效果取消了)
			return false
		}

	} else {
		//效果已经被附加过了,不能被重新附加
		return false
	}
}
//效果施加
func (self *AttributeDecResistance) PutOn(from interface{}, target effect.EffectCarrier) bool {
	//如果该效果可以被添加到对象上
	if self.EffectBase.PutOn(from, target) {

		//功能1:对于在本效果释放之后再释放的效果,直接进行抵抗
		//注册一个处理事件,到对象的 效果生效前 阶段
		self.HandlerId = target.OnBeforePutOnEffect(event.NewEventHandler(func(contextParams ...interface{}) (isCancel bool, handleResult interface{}) {
			isCancel = false //默认不取消

			//看看即将生效的效果,是不是 "属性修正效果"
			effectWantPutOn, ok := contextParams[0].(*AttributeModify)
			if ok {
				//如果是,则获取效果修正的所有属性
				allAttr := effectWantPutOn.GetAllAttr()
				for _, v := range allAttr {
					// 检查修正值是否为负数,并且进行处理
					if v.GetValue().Get() < 0.0 {
						//获取削减值的绝对值
						decVal := -v.GetValue().Get()

						//如果抵消之后,最终削弱效果<=0,则只抵消 被削弱的部分
						resistVal := math.Min(decVal, self.Amount)
						//进行抵消(对修改效果的值,进行抵消加成)
						v.GetValue().Add(resistVal, self)
					}
				}
			}
			return
		}))

		//功能2:如果在此之前,已经中了减少属性的效果,则立刻进行修正(这是因为,属性修正的效果是在putOn就已经触发了的)
		allModifyEffectsBeforeMe := target.GetAllEffects()["AttributeModify"]
		if allModifyEffectsBeforeMe != nil {
			//			fmt.Printf(" 在此之前,已经中了减少属性的效果,则立刻进行修正 %v , %v \n",allModifyEffectsBeforeMe,len(allModifyEffectsBeforeMe))
			for i, _ := range allModifyEffectsBeforeMe {
				//转化为 "属性修正效果"对象
				e, ok := allModifyEffectsBeforeMe[i].(*AttributeModify)
				if ok {
					//					fmt.Printf(" 如果是,则获取效果修正的所有属性 \n")

					//如果是,则获取效果修正的所有属性
					allAttr := e.GetAllAttr()
					for _, v := range allAttr {
						// 检查修正值是否为负数,并且进行处理(这样,当这个效果消失的时候,他会正确的恢复用户属性,而不会多加)
						if v.GetValue().Get() < 0.0 {
							//获取削减值的绝对值
							decVal := -v.GetValue().Get()

							//如果抵消之后,最终削弱效果<=0,则只抵消 被削弱的部分
							resistVal := math.Min(decVal, self.Amount)
							//进行抵消(对修改效果的值,进行抵消加成)
							v.GetValue().Add(resistVal, self)

							//改变了修正效果之后,修改用户当前属性“被属性修改效果改变的大小”
							t := target.(attribute.AttributeCarrier)
							attr := t.GetAttr(v.GetName())
							if attr != nil {
								//								fmt.Printf(" 修正被削弱的属性:%v 加成 %v \n",attr.GetName(),resistVal)
								attr.GetValue().Add(resistVal, e)
							}
						}
					}
				}
			}
		}

		return true
	} else {
		return false
	}
}