Chaosy
Tutorial Reviewer
- Joined
- Jun 9, 2011
- Messages
- 13,239
Somewhat simple creation.
Honestly a fair lot of questionmarks regarding how to use events and whatnot but at least it works.
Honestly a fair lot of questionmarks regarding how to use events and whatnot but at least it works.
JASS:
package CombatState
import DamageDetection
import LinkedList
import HashMap
import RegisterEvents
import TimerUtils
/*
Simple system to check if a unit has recently been in combat or not.
public function getTriggerCombatUnit() returns unit
public function registerOnCombatEnter(code c)
public function registerOnCombatExit(code c)
public function unit.isInCombat() returns boolean
*/
@configurable
constant LOOP_SPEED = ANIMATION_PERIOD //Standard Global
constant COMBAT_DURATION = 3
constant FRIENDLY_FIRE = true
@endconfigurable
LinkedList<unit> instances = new LinkedList<unit>()
LinkedList<onCombat> onCombatListeners = new LinkedList<onCombat>()
LinkedList<offCombat> offCombatListeners = new LinkedList<offCombat>()
HashMap<integer, real> time = new HashMap<integer, real>()
HashMap<integer, boolean> state = new HashMap<integer, boolean>()
abstract class onCombat
abstract function callback(unit u)
abstract class offCombat
abstract function callback(unit u)
public function registerOnCombatEnter(onCombat c)
onCombatListeners.push(c)
public function registerOnCombatExit(offCombat c)
offCombatListeners.push(c)
public function unit.isInCombat() returns boolean
return state.get(this.getHandleId())
function unit.getTime() returns real
return time.get(this.getHandleId())
function unit.setTime(real val)
time.put(this.getHandleId(), val)
function unit.setState(boolean inCombat)
if(this.isInCombat() != inCombat)
state.put(this.getHandleId(), inCombat)
function unit.setCombatState(boolean inCombat)
if(this.isInCombat() != inCombat)
if(inCombat == false)
for listener in offCombatListeners
listener.callback(this)
cleanup(this)
else
for listener in onCombatListeners
listener.callback(this)
instances.add(this)
this.setTime(0)
else if inCombat
this.setTime(0)
state.put(this.getHandleId(), inCombat)
function unit.isAllyWith(unit u) returns boolean
return this.getOwner().isAllyOf(u.getOwner())
function onDamage()
unit target = GetTriggerUnit()
unit source = GetEventDamageSource()
if((target.isAllyWith(source) and FRIENDLY_FIRE == true) or target.isAllyWith(source) == false)
GetTriggerUnit().setCombatState(true)
GetEventDamageSource().setCombatState(true)
function cleanup(unit u)
integer h = u.getHandleId()
state.remove(h)
time.remove(h)
instances.remove(u)
function onLoop()
real time
for unit u in instances
time = u.getTime()
if(time >= COMBAT_DURATION)
for listener in offCombatListeners
listener.callback(u)
cleanup(u)
else
u.setTime(time + LOOP_SPEED)
function onDeath()
cleanup(GetTriggerUnit())
init
registerPlayerUnitEvent(EVENT_PLAYER_UNIT_DEATH, function onDeath)
enableDamageDetect() // Not sure if needed
addOnDamageFunc(function onDamage)
getTimer().startPeriodic(LOOP_SPEED, function onLoop)
Demo
JASS:
package Hello
import CombatState
import OnUnitEnterLeave
import UnitIndexer
group g = CreateGroup()
function onMapEnter()
g.addUnit(getEnterLeaveUnit())
function onMapLeave()
g.removeUnit(getEnterLeaveUnit())
function onLoop()
for unit u in g
if(u.getHP() < u.getMaxHP() and u.isInCombat() == false)
u.setHP(u.getHP() + 1)
CreateTextTag()
..setPos(u.getPos3Fly())
..setColor(0, 255, 0, 255)
..setLifespan(0.5)
..setFadepoint(0.25)
..setPermanent(false)
..setText("+1", 10)
..setVelocity(0, 0.4)
init
registerOnCombatEnter() u ->
print(u.getName() + " entered combat!")
registerOnCombatExit() u ->
print(u.getName() + " left combat!")
onUnitIndex(function onMapEnter)
onUnitDeindex(function onMapLeave)
CreateTimer().startPeriodic(0.1, function onLoop)
Last edited: