AdvBuff.lua 11.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428

local Buff = class("Buff")

Buff.HP_CHANGE = 1  --生命变化(每回合生效)
Buff.HP_MAX_CHANGE = 2	--生命上限变化(状态)
Buff.ATTR_CHANGE = 3 --属性变化(状态)
Buff.IMMNUE_ATK = 4 -- 免疫普通攻击
Buff.BACK_HURT = 5 -- 伤害反弹
Buff.HURT_CHANGE = 6 -- 伤害变化
Buff.INJURED_CHANGE = 7 -- 受伤变化
Buff.HURT_TRANSFER = 8 -- 侍宠(转移自己受到的伤害)
Buff.HURT_ABSORB = 9 -- 舍身(吸收他人受到的伤害)
Buff.CANT_BACK_ATK = 10 -- 无法反击
Buff.IMMNUE_BUFF = 11 -- 免疫buff
Buff.CLEAR_BUFF = 12 -- 清除buff
Buff.CANT_SKILL = 13 -- 禁止技能
Buff.OPEN_BLOCK = 14 -- 翻开格子(每回合)
Buff.POWER_CHANGE = 15 -- MP变化(每回合)
Buff.HP_CHANGE_NOW = 16 -- 生命变化(每回合生效,立刻生效)

--角色一些属性的变化
local function commonAttr(_Buff, attrName)
	_Buff._init = function(self, data) --初始化变化值
		self.owner:reSetAttr(attrName)
	end
	_Buff._effectValue = function(self)
		return self.buffData.effectValue1, self.buffData.effectValue2
	end
	_Buff._endBuff = function(self, data)
		self.owner:reSetAttr(attrName)
	end
end

local BuffFactory = {
	[Buff.HP_CHANGE] = function(_Buff)
		_Buff._init = function(self, data) --初始化变化值
			self._changeV = 0
			if self.buffData.effectValue1 == 0 then --固定值
				self._changeV = self.buffData.effectValue2
			elseif self.buffData.effectValue1 == 1 then
				local baseOwner = self.buffData.effectValue4 == 1 and self.owner or self.release
				local attrs = {[0] = "hp", [1] = "hpMax", [2] = "atk"}
				self._changeV = baseOwner[attrs[self.buffData.effectValue3]] * self.buffData.effectValue2 / 100
			end
			if self._changeV < 0 then
				if self.release then
					self._changeV = -self.release:getHurtValue(-self._changeV)
				end
			end
		end
		_Buff._initDB = function(self, data) 
			self._changeV = data.cv
		end
		_Buff._afterRound = function(self)
			local value = self:effect()
			if value > 0 then
				self.owner:recover(value, self.release)
			elseif value < 0 then
				self.owner:hurt(-value, self.release, {hurtType = 2})
			end
		end
		_Buff._afterTurn = function(self)
			local value = self:effect()
			if value > 0 then
				self.owner:recover(value, self.release)
			elseif value < 0 then
				self.owner:hurt(-value, self.release, {hurtType = 2})
			end
		end
		_Buff._effectValue = function(self)
			return self._changeV
		end
		_Buff._getDB = function(self)
			return {cv = self._changeV}
		end
	end,

	[Buff.HP_MAX_CHANGE] = function(_Buff)
		_Buff._init = function(self, data) --初始化变化值
			self._changeV = 0
			if self.buffData.effectValue1 == 0 then --固定值
				self._changeV = self.buffData.effectValue2
			elseif self.buffData.effectValue1 == 1 then
				local baseOwner = self.buffData.effectValue4 == 1 and self.owner or self.release
				local attrs = {[0] = "hp", [1] = "hpMax", [2] = "atk"}
				self._changeV = baseOwner[attrs[self.buffData.effectValue3]] * self.buffData.effectValue2 / 100
			end
			local old = self.owner.hpMax
			self.owner.hpMax = math.max(1, self.owner.hpMax + self._changeV)
			self._changeV = self.owner.hpMax - old
			if self._changeV > 0 then
				self.owner:recover(self._changeV, self.release) -- 防止release不存在,地图点buff
			elseif self._changeV < 0 then
				self.owner:hurt(self.release and self.release:getHurtValue(-self._changeV) or -self._changeV, self.release, {hurtType = 2})
				self.owner.hp = math.min(self.owner.hpMax, self.owner.hp)
			end
		end
		_Buff._initDB = function(self, data)
			self._changeV = data.cv
		end
		_Buff._endBuff = function(self, data)
			if self._changeV then
				self.owner.hpMax = math.max(1, self.owner.hpMax - self._changeV)
				self.owner.hp = math.min(self.owner.hpMax, self.owner.hp)
			end
		end
		_Buff._getDB = function(self)
			return {cv = self._changeV}
		end
	end,
	[Buff.ATTR_CHANGE] = function(_Buff)
		local attrName = AttsEnumEx[_Buff.buffData.effectValue3]
		commonAttr(_Buff, attrName)
	end,
	[Buff.BACK_HURT] = function(_Buff)
		_Buff._effectValue = function(self)
			return self.buffData.effectValue1, self.buffData.effectValue2, self.buffData.effectValue3
		end
	end,

	[Buff.HURT_CHANGE] = function(_Buff)
		_Buff._effectValue = function(self)
			return self.buffData.effectValue1, self.buffData.effectValue2
		end
	end,

	[Buff.INJURED_CHANGE] = function(_Buff)
		_Buff._effectValue = function(self)
			return self.buffData.effectValue1, self.buffData.effectValue2
		end
	end,

	[Buff.HURT_TRANSFER] = function(_Buff)
		_Buff._effectValue = function(self)
			return self.buffData.effectValue1, self.buffData.effectValue2
		end
	end,

	[Buff.HURT_ABSORB] = function(_Buff)
		_Buff._effectValue = function(self)
			return self.buffData.effectValue1, self.buffData.effectValue2
		end
	end,

	[Buff.IMMNUE_BUFF] = function(_Buff)
		_Buff._init = function(self, data)
			self.count = self.buffData.effectValue3
		end
		_Buff._canEffect = function(self, buffId, buffGroup)
			local cType, aim = self.buffData.effectValue1, self.buffData.effectValue2
			if (cType == 0 and buffId == aim) or (cType == 1 and buffGroup == aim) then
				return true
			end
		end
	end,

	[Buff.CLEAR_BUFF] = function(_Buff)
		_Buff._init = function(self, data)
			self.count = self.buffData.effectValue3
			for _, buff in ipairs(self.buffs) do -- 挂上就清除一下子
				if not buff.isDel and self:canEffect(buff.id, buff:getGroup()) then
					buff.isDel = true
					self:effect()
				end
			end 
		end
		_Buff._canEffect = function(self, buffId, buffGroup)
			local cType, aim = self.buffData.effectValue1, self.buffData.effectValue2
			if (cType == 0 and buffId == aim) or (cType == 1 and buffGroup == aim) then
				return true
			end
		end
	end,

	[Buff.OPEN_BLOCK] = function(_Buff)
		_Buff._afterRound = function(self)
			local roomNum = self:effect()
			self.owner.battle.adv:openBlockRand(roomNum)
		end
		_Buff._afterTurn = function(self)
			local roomNum = self:effect()
			self.owner.battle.adv:openBlockRand(roomNum)
		end
		_Buff._effectValue = function(self)
			return self.buffData.effectValue1
		end
	end,

	[Buff.POWER_CHANGE] = function(_Buff)
		--cType 0 or nil  值  1 百分比
		_Buff._afterRound = function(self)
			local value, cType = self:effect()
			self.owner.battle.adv:changePower(value, cType)
		end
		_Buff._afterTurn = function(self)
			local value, cType = self:effect()
			self.owner.battle.adv:changePower(value, cType)
		end
		_Buff._effectValue = function(self)
			return self.buffData.effectValue2, self.buffData.effectValue1
		end
	end,
	[Buff.HP_CHANGE_NOW] = function(_Buff)
		_Buff._init = function(self, data) --初始化变化值
			self._changeV = 0
			if self.buffData.effectValue1 == 0 then --固定值
				self._changeV = self.buffData.effectValue2
			elseif self.buffData.effectValue1 == 1 then
				local baseOwner = self.buffData.effectValue4 == 1 and self.owner or self.release
				local attrs = {[0] = "hp", [1] = "hpMax", [2] = "atk"}
				self._changeV = baseOwner[attrs[self.buffData.effectValue3]] * self.buffData.effectValue2 / 100
			end
			if self._changeV < 0 then
				self._changeV = -self.release:getHurtValue(-self._changeV)
			end
			self:afterTurn()
		end
		_Buff._initDB = function(self, data) 
			self._changeV = data.cv
		end
		_Buff._afterRound = function(self)
			local value = self:effect()
			if value > 0 then
				self.owner:recover(value, self.release)
			elseif value < 0 then
				self.owner:hurt(-value, self.release, {hurtType = 2})
			end
		end
		_Buff._afterTurn = function(self)
			local value = self:effect()
			if value > 0 then
				self.owner:recover(value, self.release)
			elseif value < 0 then
				self.owner:hurt(-value, self.release, {hurtType = 2})
			end
		end
		_Buff._effectValue = function(self)
			return self._changeV
		end
		_Buff._getDB = function(self)
			return {cv = self._changeV}
		end
	end,
}

function Buff:ctor(owner, id)
	self.owner = owner
	self.id = id
	self.buffData = csvdb["adv_buffCsv"][self.id]
	self.isDel = false
	self.roundSpace = 0 --生效间隔
	self.turnSpace = 0 --生效间隔
	self.round = 0 --剩余的回合
	self.turn = 0  --剩余战斗内回合
	self.count = -1 -- 可生效的次数  -1 无次数限制

	if BuffFactory[self.buffData.type] then
		BuffFactory[self.buffData.type](self)
	end
end

function Buff.create(owner, release, data)
	local buff = Buff.new(owner, data.id)
	buff:initNew(release, data)
	return buff
end

function Buff.load(owner, data)
	local buff = Buff.new(owner, data.id)
	buff:initByDB(data)
	return buff
end

function Buff:initNew(release, data)
	self.release = release or self.owner
	self.round = self.buffData.round
	self.turn = self.buffData.turn
	if self.buffData.effectTime > 0 then
		self.count = self.buffData.effectTime
	end
	if self._init then
		self:_init(data)
	end
end

function Buff:initByDB(data)
	if data.rele then
		if data.rele == 0 then
			self.release = self.owner.battle.player
		else
			self.release = self.owner.battle:getEnemyById(data.rele)
		end
	end
	self.round = data.round
	self.roundSpace = data.roundSp -- 可以优化为0的时候不记录
	if data.count then
		self.count = data.count
	end

	if self._initDB then
		self:_initDB(data)
	end
end

function Buff:battleEnd()
	if self.buffData.turn ~= 0 then
		self.isDel = true -- turn类型buff战斗结束后移除
	end
end

function Buff:beforeTurn()
	if self.isDel or self.owner.isDead or self.buffData.turn == 0 then return end
	if self.turnSpace > 0 then
		return
	end
	if self._beforeTurn then
		self:_beforeTurn()
	end
end

function Buff:afterTurn()
	if self.isDel or self.owner.isDead or self.buffData.turn == 0 then return end
	if self.turnSpace > 0 then
		self.turnSpace = self.turnSpace - 1
		self:decTurn()
		return
	end
	if self._afterTurn then
		self:_afterTurn()
	end
	if self.buffData.turnTime > 0 then
		self.turnSpace = self.buffData.turnTime
	end
	self:decTurn()
end

function Buff:decTurn()
	if self.buffData.turn == 0 then
		return
	end
	self.turn = self.turn - 1
	if self.turn <= 0 then
		self.isDel = true
	end
end

function Buff:afterRound()
	if self.isDel or self.owner.isDead or self.buffData.round == 0 then return end
	if self.roundSpace > 0 then
		self.roundSpace = self.roundSpace - 1
		self:decRound()
		return
	end
	if self._afterRound then
		self:_afterRound()
	end
	if self.buffData.roundTime > 0 then
		self.roundSpace = self.buffData.roundTime
	end
	self:decRound()
end

function Buff:decRound()
	if self.buffData.round == 0 then
		return
	end
	self.round = self.round - 1
	if self.round <= 0 then
		self.isDel = true
	end
end

function Buff:canEffect(...)
	if not self._canEffect then
		return true
	end
	return self:_canEffect(...)
end

function Buff:effect()
	self:decCount()
	if self._effectValue then
		return self:_effectValue()
	end
end
--删除buff 时调用
function Buff:endBuff()
	if self._endBuff then
		self:_endBuff()
	end
end

function Buff:getType()
	return self.buffData.type
end

function Buff:decCount()
	if self.count == -1 then return end
	self.count = self.count - 1
	if self.count <= 0 then
		self.isDel = true
	end
end

function Buff:getGroup()
	return self.buffData.group
end

function Buff:getDB()
	local db = {}
	if self._getDB then
		db = self:_getDB()
	end
	db.id = self.id
	if self.release and not self.release.isDead then
		db.rele = self.release.id or 0 --释放者的id  (0 为玩家)  (不存在 则释放者不存在或者已经死亡)
	end
	if self.buffData.round ~= 0 then
		db.round = self.round
	end
	db.roundSp = self.roundSpace
	if self.count ~= -1 then
		db.count = self.count
	end
	return db
end

return Buff