Blame view

src/adv/AdvPlayer.lua 13.2 KB
46fac6f1   zhouahaihai   酱料
1
2
3
4
5
6
7
8
9
10
  -- 角色
  local Buff = require "adv.AdvBuff"
  local Passive = require "adv.AdvPassive"
  
  local BaseObject = class("BaseObject")
  function BaseObject:ctor(battle)
  	self.battle = battle
  	self.hpMax = 0
  	self.hp = 0
  	self.atk = 0
e996b82a   zhouahaihai   冒险增加防御属性
11
  	self.def = 0
46fac6f1   zhouahaihai   酱料
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  	self.aType = 0 --攻击类型
  	self.lock = nil
  	self.passives = {} --固有技能
  	self.buffs = {} --buff
  	self.isDead = false
  end
  --初始化角色
  function BaseObject:initData(data)
  	self.hpMax = data.hpMax or data.hp
  	self.hp = data.hp
  	--可变化的值
  	self.atk = data.atk
  	self.miss = data.miss
  	self.hit = data.hit
e996b82a   zhouahaihai   冒险增加防御属性
26
  	self.def = data.def
46fac6f1   zhouahaihai   酱料
27
28
29
30
  	--基础值记录
  	self._atk = data._atk or self.atk
  	self._miss = data._miss or self.miss
  	self._hit = data._hit or self.hit
e996b82a   zhouahaihai   冒险增加防御属性
31
  	self._def = data._def or self.def
46fac6f1   zhouahaihai   酱料
32
33
34
35
  end
  -- 角色初始化完以后才是 技能和被动技能  方便初始化 buff 的 释放对象
  function BaseObject:initAfter(data)
  	for _, passive in ipairs(data.passives or {}) do
36c30c5c   zhouahaihai   冒险
36
  		table.insert(self.passives, Passive.new(self, passive))
46fac6f1   zhouahaihai   酱料
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  	end
  	for _, buff in ipairs(data.buffs or {}) do
  		table.insert(self.buffs, Buff.load(self, buff))
  	end
  end
  
  function BaseObject:afterRound()
  	for _, passive in ipairs(self.passives) do
  		passive:afterRound(self)
  	end
  	for _, buff in ipairs(self.buffs) do
  		buff:afterRound(self)
  	end
  end
  
  function BaseObject:clearRound()
  	for i = #self.passives, 1, -1 do
  		if self.passives[i].isDel then
  			self.passives[i]:endPassive()
  			table.remove(self.passives, i)
  		end
  	end
  	for i = #self.buffs, 1, -1 do
  		if self.buffs[i].isDel then
36c30c5c   zhouahaihai   冒险
61
  			self.battle.adv:backBuff(self.id, self.buffs[i].id, true)
46fac6f1   zhouahaihai   酱料
62
63
64
65
66
67
68
69
70
71
  			self.buffs[i]:endBuff()
  			table.remove(self.buffs, i)
  		end
  	end
  end
  function BaseObject:clear()
  	self.buffs = {}
  	self.passives = {}
  end
  
23a38f47   suhongyang   new passives effect
72
73
74
75
76
77
78
  function BaseObject:addPassive(params)
  	local skillId = params.id
  	local skillData = csvdb["adv_skill_passiveCsv"][skillId]
  	if not skillData then return end
  	local level = params.level or 1
  	if not skillData[level] then return end
  	table.insert(self.passives, Passive.new(self, { id = skillId, level = level }))
4eadc2ab   suhongyang   獲得被動暫時增加log,後面可以去掉
79
80
  
  	self.battle.adv:backPassive(self.id, skillId)
23a38f47   suhongyang   new passives effect
81
82
  end
  
46fac6f1   zhouahaihai   酱料
83
84
85
86
87
88
89
90
91
92
93
94
95
  function BaseObject:addBuff(buffId, releaser)
  	local buffData = csvdb["adv_buffCsv"][buffId]
  	if not buffData then return end
  	for _, buff in ipairs(self.buffs) do
  		if not buff.isDel and (buff:getType() == CLEAR_BUFF or buff:getType() == IMMNUE_BUFF) then
  			local cType, aim = buff:effectValue() -- 0=buffid 1=buff组
  			if (cType == 0 and buffId == aim) or (cType == 1 and buffData.group == aim) then  -- buff 剔除
  				buff:decCount() --减少次数
  				return
  			end
  		end
  	end
  	table.insert(self.buffs, Buff.create(self, releaser, {id = buffId}))
36c30c5c   zhouahaihai   冒险
96
97
  
  	self.battle.adv:backBuff(self.id, buffId)
46fac6f1   zhouahaihai   酱料
98
99
100
101
102
103
104
105
106
107
  end
  
  function BaseObject:hadBuff(bType)
  	for _, buff in ipairs(self.buffs) do
  		if not buff.isDel and buff:getType() == bType then
  			return true
  		end
  	end
  end
  
1cb78a24   suhongyang   passive的筛选
108
109
110
111
112
113
114
115
  function BaseObject:hadBuffById(bId)
  	for _, buff in ipairs(self.buffs) do
  		if not buff.isDel and buff.id == bId then
  			return true
  		end
  	end
  end
  
46fac6f1   zhouahaihai   酱料
116
117
118
119
120
121
122
  -- 通用的buff 效果汇总  -- 0 固定 1百分比  两种分类
  function BaseObject:getCommonBuffEffect(bType)
  	local effect, count = {[0] = 0, [1] = 0}, 0
  	for _, buff in ipairs(self.buffs) do
  		if not buff.isDel and buff:getType() == bType then
  			local cType, value = buff:effectValue()
  			if cType then
4ae223df   zhouahaihai   Buff bug
123
  				effect[cType] = effect[cType] + value
46fac6f1   zhouahaihai   酱料
124
125
126
127
  				count = count + 1
  			end
  		end
  	end
4ae223df   zhouahaihai   Buff bug
128
  	effect[1] = effect[1] / 100
46fac6f1   zhouahaihai   酱料
129
130
131
132
133
134
135
136
137
138
  	return effect, count --效果 和生效的buff 个数
  end
  --伤害反弹
  function BaseObject:getBackHurtBuff(isAtk)
  	local effect = {[0] = 0, [1] = 0}
  	for _, buff in ipairs(self.buffs) do
  		if not buff.isDel and buff:getType() == Buff.BACK_HURT then
  			local cType, value, aType = buff:effectValue()  --  aType 0 全部  1 普通攻击
  			if cType then
  				if aType == 0 or isAtk then
4ae223df   zhouahaihai   Buff bug
139
  					effect[cType] = effect[cType] + value
46fac6f1   zhouahaihai   酱料
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
  				end
  			end
  		end
  	end
  	return effect
  end
  
  --释放者伤害变化
  function BaseObject:getHurtChange()
  	local change = self:getCommonBuffEffect(Buff.HURT_CHANGE)
  	return change
  end
  --受伤者受伤变化
  function BaseObject:getInjuredChange()
  	local change = self:getCommonBuffEffect(Buff.INJURED_CHANGE)
  	return change
  end
  --重新计算属性
  function BaseObject:reSetAttr(field)
36c30c5c   zhouahaihai   冒险
159
  	local old = self[field]
46fac6f1   zhouahaihai   酱料
160
  	self[field] = self["_" .. field] --重置一下
e996b82a   zhouahaihai   冒险增加防御属性
161
  	local fieldToBuff = {atk = Buff.IMMNUE_ATK, hit = Buff.HIT_CHANGE, miss = Buff.MISS_CHANGE, def = Buff.DEF_CHANGE}
46fac6f1   zhouahaihai   酱料
162
  	local effect = self:getCommonBuffEffect(fieldToBuff[field])
e10edb5f   zhouahaihai   冒险事件新
163
  	self[field] = math.ceil((self[field] + effect[0]) * (1 + effect[1]))
36c30c5c   zhouahaihai   冒险
164
165
166
167
  	local delta = self[field] - old
  	if delta ~= 0 then
  		if field == "atk" then
  			self.battle.adv:backAtkChange(self.id, delta)
e996b82a   zhouahaihai   冒险增加防御属性
168
169
  		elseif field == "def" then
  			self.battle.adv:backDefChange(self.id, delta)
36c30c5c   zhouahaihai   冒险
170
171
  		end 
  	end
46fac6f1   zhouahaihai   酱料
172
173
174
175
176
177
178
179
180
181
182
  end
  
  --计算打出伤害加成后的值 
  function BaseObject:getHurtValue(value)
  	value = value or self.atk
  	local hurtChange = self:getHurtChange()
  	return math.max(0, (value + hurtChange[0]) * (1 + hurtChange[1]))
  end
  --计算自己伤害减免后的值
  function BaseObject:getInjuredValue(value)
  	local injuredChange = self:getInjuredChange()
e996b82a   zhouahaihai   冒险增加防御属性
183
  	return math.max(0, (value + injuredChange[0]) * (1 + injuredChange[1]) - self.def)
46fac6f1   zhouahaihai   酱料
184
185
186
  end
  
  --最终伤害 = [ 敌方攻击 * (1+伤害增加百分比-伤害减少百分比)*(1+受伤增加百分比-受伤减少百分比)+(伤害增加固定值-伤害增加固定值+受伤增加固定值-受伤增加固定值)]*(1+侍宠百分比)-侍宠固定值
ec87b4a5   zhouahaihai   冒险 完善
187
  -- params   -- hurtType  1 普攻伤害  2 buff伤害  3 反弹伤害  4 真实伤害
46fac6f1   zhouahaihai   酱料
188
189
190
191
  --进入这个方法之前计算好释放者加成的伤害
  function BaseObject:hurt(value, releaser, params)
  	params = params or {}
  	if params.hurtType and params.hurtType == 1 then
02c4de8d   zhouahaihai   增加 固有技
192
193
194
195
  		releaser:triggerPassive(Passive.SELF_ATK)
  		for _, team in ipairs(releaser:getTeam(1, true)) do
  			team:triggerPassive(Passive.TEAM_ATK)
  		end
46fac6f1   zhouahaihai   酱料
196
197
198
199
200
201
  		if self:hadBuff(Buff.IMMNUE_ATK) then return end --无视普通攻击
  		
  		local hit = releaser.hit - self.miss  --命中率
  		if hit < math.randomInt(1, 100) then --miss
  			return
  		end
02c4de8d   zhouahaihai   增加 固有技
202
203
204
205
  		self:triggerPassive(Passive.SELF_HURT, {trigger = releaser})
  		for _, team in ipairs(self:getTeam(1, true)) do
  			team:triggerPassive(Passive.TEAM_HURT, {trigger = releaser})
  		end
46fac6f1   zhouahaihai   酱料
206
  	end
ec87b4a5   zhouahaihai   冒险 完善
207
208
209
  	if not params.hurtType or params.hurtType ~= 4 then
  		value = self:getInjuredValue(value) --减伤计算
  	end
46fac6f1   zhouahaihai   酱料
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
  	if value == 0 then return end
  
  	-- 舍身和恃宠
  	local team = self:getTeam(1)
  	local transfer = {}
  	local absorb = {}
  	for _, one in ipairs(team) do
  		local change1, count1 = one:getCommonBuffEffect(Buff.INJURED_CHANGE)
  		local change2, count2 = one:getCommonBuffEffect(Buff.HURT_ABSORB)
  		if count1 > 0 then
  			table.insert(transfer, {one, change1, count1})
  		end
  		if count2 > 0 then
  			table.insert(absorb, {one, change2, count2})
  		end
  	end
  	if #absorb == 1 then --舍身优先级高  --舍身生效
  		if absorb[1][1] ~= self then --舍身的人不是自己才有效
  			local absorbV = (value - absorb[1][2][0]) * absorb[1][2][1] + absorb[1][2][0]  --固定值先生效
  			value = value - absorbV
  			absorb[1][1]:hurt(absorbV, releaser, params)
  		end
  	else
  		if #transfer == 1 and transfer[1][1] == self and #team > 1 then --侍宠 生效  
  			local transferValue = (value - transfer[1][2][0])* transfer[1][2][1] + transfer[1][2][0] --固定值先生效
  			value = value - transferValue
  			local oneValue = transferValue / (#team - 1)
  			for _, one in ipairs(team) do
  				if one ~= self then
  					one:hurt(oneValue, releaser, params)
  				end
  			end
  		end
  	end
  
e10edb5f   zhouahaihai   冒险事件新
245
  	value = math.max(0, math.ceil(value))
46fac6f1   zhouahaihai   酱料
246
247
248
249
250
251
252
253
254
  	if value == 0 then return end
  	-- 反弹伤害
  	if params.hurtType ~= 3 and releaser and not releaser.isDead then
  		local backEffect = self:getBackHurtBuff(params.hurtType == 1)
  		local backValue = math.max(0, value * backEffect[1] + backEffect[0])
  		releaser:hurt(backValue, releaser, {hurtType = 3})
  	end
  
  	--受伤了~
36c30c5c   zhouahaihai   冒险
255
  	self.battle.adv:backHpChange(self.id, -value)
46fac6f1   zhouahaihai   酱料
256
  	self.hp = math.max(0, self.hp - value)
b0fe1817   zhouahaihai   冒险分数
257
258
259
260
261
  
  	if self.cutHp then
  		self:cutHp(value)
  	end
  
46fac6f1   zhouahaihai   酱料
262
  	if self.hp == 0 then
02c4de8d   zhouahaihai   增加 固有技
263
264
265
266
267
  		self:triggerPassive(Passive.SELF_DEAD)
  		for _, team in ipairs(self:getTeam(1, true)) do
  			team:triggerPassive(Passive.TEAM_DEAD)
  		end
  
46fac6f1   zhouahaihai   酱料
268
  		self.isDead = true
bedca62d   zhouahaihai   冒险
269
  		self.battle.adv:backDead(self.id)
46fac6f1   zhouahaihai   酱料
270
  	end
02c4de8d   zhouahaihai   增加 固有技
271
272
273
274
  	self:triggerPassive(Passive.HURT_PERCENT_SELF, {value = value / self.hpMax})
  	for _, team in ipairs(self:getTeam(1, true)) do
  		team:triggerPassive(Passive.HURT_PERCENT_TEAM, {value = value / self.hpMax})
  	end
46fac6f1   zhouahaihai   酱料
275
276
277
278
  end
  --恢复
  function BaseObject:recover(value, releaser, params)
  	params = params or {}
e10edb5f   zhouahaihai   冒险事件新
279
  	value = math.max(0, math.ceil(value))
46fac6f1   zhouahaihai   酱料
280
  	self.hp = math.min(self.hpMax, self.hp + value)
36c30c5c   zhouahaihai   冒险
281
  	self.battle.adv:backHpChange(self.id, value)
46fac6f1   zhouahaihai   酱料
282
283
284
  end
  
  function BaseObject:releaseSkill(skillId, skillLevel, target)
36c30c5c   zhouahaihai   冒险
285
  	if self:hadBuff(Buff.CANT_SKILL) then return end -- 针对 怪物
46fac6f1   zhouahaihai   酱料
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
  	skillLevel = skillLevel or 1
  	local skillData = csvdb["adv_skillCsv"][skillId][skillLevel or 1]
  	local targetChoose = skillData.target:toArray(true, "=")
  	local targetNum = skillData.targetNum:toArray(true, "=")
  	--确定目标
  	local targets = {}
  	if targetChoose[1] == 0 then --选定一个
  		assert(target, "error " .. self.class.__cname .. "  releaseSkill id: " .. skillId .." targetChoose==0 but no target")
  		table.insert(targets, target)
  	elseif targetChoose[1] == 1 then --全部
  		if not targetChoose[2] or targetChoose[2] == 0 then
  			targets = self:getTeam(0)
  		else --指定 monsterId
  			local team = self:getTeam(0)
  			for _, one in pairs(team) do
  				if one.monsterId and one.monsterId == targetChoose[2] then
  					table.insert(targets, one)
  				end
  			end
  		end
  	elseif targetChoose[1] == 2 then -- 我方
  		if targetChoose[2] == 1 then
  			table.insert(targets, self)
  		elseif targetChoose[2] == 2 then
  			targets = self:getTeam(1, true)
  		elseif targetChoose[2] == 3 then
  			targets = self:getTeam(1)
  		end 
  	elseif targetChoose[1] == 3 then -- 敌方
  		targets = self:getTeam(2)
  	end
  
  	if targetChoose[1] ~= 0 then  -- 不是指定一个的进行 排序 和 数量筛选
  		local tempT = targets
  		local targets = {}
  		local function randNum(isSort) 
  			if #tempT <= targetNum[2] then
  				targets = tempT
  				return
  			end 
  			if isSort then
  				for i = 1, targetNum[2] do
  					table.insert(targets, tempT[i])
  				end
  			else
  				for i = 1, targetNum[2] do
  					local idx = math.randomInt(1, #tempT)
  					table.insert(targets, tempT[idx])
  					table.remove(tempT, idx)
  				end
  			end
  		end
  		if targetNum[1] == 0 then --任意排序
  			randNum(false)
  		elseif targetNum[1] == 1 then --血量从低到高
  			table.sort(tempT, function(o1, o2)
  				return o1.hp < o2.hp
  			end)
  			randNum(true)
  		elseif targetNum[1] == 2 then --血量从高到低
  			table.sort(tempT, function(o1, o2)
  				return o1.hp > o2.hp
  			end)
  			randNum(true)
  		else
  			targets = tempT  --all
  		end
  	end
36c30c5c   zhouahaihai   冒险
354
355
356
357
  	--返回客户端
  	for _, target in ipairs(targets) do
  		self.battle.adv:backSkill(self.id, skillId, target.id)
  	end
46fac6f1   zhouahaihai   酱料
358
359
360
361
362
363
364
365
366
367
  	-- 增加buff
  	for _, buffId in ipairs(skillData.selfbuff:toArray(true, "=")) do
  		self:addBuff(buffId, self)
  	end
  
  	for _, buffId in ipairs(skillData.targetbuff:toArray(true, "=")) do
  		for _, target_ in ipairs(targets) do
  			target_:addBuff(buffId, self)
  		end
  	end
02c4de8d   zhouahaihai   增加 固有技
368
369
370
371
372
373
374
  
  	for _, team in ipairs(self:getTeam(2)) do
  		team:triggerPassive(Passive.TARGET_SKILL)
  	end
  	for _, team in ipairs(self:getTeam(1, true)) do
  		team:triggerPassive(Passive.TEAM_SKILL)
  	end
46fac6f1   zhouahaihai   酱料
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
  end
  
  --0 全部  1 我方  2 敌方
  function BaseObject:getTeam(nType, noSelf)
  	nType = nType or 0
  	local team = {}
  	local function addPlayer()
  		if not noSelf or self.battle.player ~= self then
  			if not self.battle.player.isDead then
  				table.insert(team, self.battle.player)
  			end
  		end
  	end
  	local function addEnemy()
  		for _, enemy in pairs(self.battle.enemys) do
  			if not noSelf or enemy ~= self then 
  				if not enemy.isDead and not enemy.lock then -- 已经翻开的
  					table.insert(team, enemy)
  				end
  			end
  		end
  	end
  	if nType == 0 then
  		addPlayer()
  		addEnemy()
  	elseif nType == 1 then
  		if self.class == Player then --玩家
  			addPlayer()
  		else
  			addEnemy()
  		end
  	elseif nType == 2 then
  		if self.class == Player then --玩家
  			addEnemy()
  		else
  			addPlayer()
  		end
  	end
  	return team
  end
  
  function BaseObject:getDB()
  	local db = {}
  	db.hpMax = self.hpMax
  	db.hp = self.hp
e996b82a   zhouahaihai   冒险增加防御属性
420
  	local baseAttr = {"atk", "miss", "hit", "def"}
46fac6f1   zhouahaihai   酱料
421
422
423
424
425
426
427
428
  	for _, field in pairs(baseAttr) do
  		db[field] = self[field]
  		db["_"..field] = self["_" .. field]
  	end
  	db.passives = {}
  	for _, passive in ipairs(self.passives) do
  		table.insert(db.passives, passive:getDB())
  	end
4ae223df   zhouahaihai   Buff bug
429
  	db.buffs = {}
46fac6f1   zhouahaihai   酱料
430
431
432
433
434
435
436
  	for _, buff in ipairs(self.buffs) do
  		table.insert(db.buffs, buff:getDB())
  	end
  	return db
  end
  
  function BaseObject:triggerPassive(condType, params)
02c4de8d   zhouahaihai   增加 固有技
437
438
439
440
  	if self.isDead then return end
  	for _, passive in ipairs(self.passives) do
  		passive:trigger(condType, params) --检查触发
  	end
46fac6f1   zhouahaihai   酱料
441
442
443
  end
  
  local Enemy = class("Enemy", BaseObject)
36c30c5c   zhouahaihai   冒险
444
  function Enemy:ctor(battle, mId, monsterId, roomId, blockId, lock, enemy)
46fac6f1   zhouahaihai   酱料
445
  	Enemy.super.ctor(self, battle)
36c30c5c   zhouahaihai   冒险
446
447
  	self.id = mId
  	self.monsterId = monsterId  --数据id
46fac6f1   zhouahaihai   酱料
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
  	self.roomId = roomId
  	self.blockId = blockId
  	self.lock = lock
  	self:initData(enemy)
  end
  function Enemy:unlock(id)
  	self.id = id
  	self.lock = nil
  end
  
  local Player = class("Player", BaseObject)
  function Player:ctor(battle, data)
  	Player.super.ctor(self, battle)
  	self:initData(data)
  end
  
b0fe1817   zhouahaihai   冒险分数
464
465
466
467
  function Player:cutHp(value)
  	self.battle.adv:scoreChange(AdvScoreType.Hurt, value)
  end
  
36c30c5c   zhouahaihai   冒险
468
  return table.pack(Player, Enemy)