Narks
Vastly intelligent whale-like being from the stars
- Reaction score
- 90
I'm working on a projectile system, because other projectile systems I am simply not familiar with, and they do not suit my needs. I am aware this is unfinished - but honestly, I have no idea if I'm going in the right direction.
My ultimate goal, is to make a projectile system, where you just specify projectile target, speed, friction, homing, model, damage, etc., and it launches the projectile and you don't have to worry about it. (although if there is a way I can incorporate having the projectile call a function on collision or on tick or something, that would be cool too).
Alright, my first round of questions.
1). How do I check if a struct has been allocated? You can see I am checking if the unit is null to see if the struct has been destroyed, but I'm sure there is a proper way.
My ultimate goal, is to make a projectile system, where you just specify projectile target, speed, friction, homing, model, damage, etc., and it launches the projectile and you don't have to worry about it. (although if there is a way I can incorporate having the projectile call a function on collision or on tick or something, that would be cool too).
JASS:
library NarksProjectiles initializer Init requires Common
globals
private constant integer DUMMY_ID = 039;pZZZ039;
endglobals
struct Projectile
unit Unit
private effect Model
real bearing
real velocity
real lifetime
static method altCreate takes player p returns Projectile
local Projectile pr = Projectile.create()
set pr.Unit = CreateUnit(p,DUMMY_ID,0,0,0)
call SetUnitHeight(pr.Unit, 128)
set pr.bearing = 0
set pr.velocity = 0
set pr.lifetime = 0
return pr
endmethod
method onDestroy takes nothing returns nothing
set this.Unit = null
call DestroyEffect(Model)
endmethod
method tick takes nothing returns nothing
if GetUnitState(.Unit, UNIT_STATE_LIFE) <= 0 then
call .destroy()
return
endif
call SetUnitXY(this.Unit, GetUnitX(.Unit) + .velocity * Cos(bearing * bj_DEGTORAD), GetUnitY(.Unit) + .velocity * Sin(bearing * bj_DEGTORAD))
endmethod
method setModel takes string s returns nothing
if Model != null then
call DestroyEffect(Model)
endif
set Model = AddSpecialEffectTarget(s, .Unit, "origin")
endmethod
endstruct
globals
private timer Timer
private integer Top = 0
private Projectile array arProj
endglobals
private function Update takes nothing returns nothing
local integer i = Top
debug call BJDebugMsg("Top is " + I2S(Top))
loop
exitwhen i == 0
if arProj<i>.Unit == null then
// Projectile is dead, lets dispose of it.
if i == Top then
// Projectile was on top, just lower the top count.
set Top = Top - 1
else
// Projectile was not on top, lets fill the hole.
set arProj<i> = arProj[Top]
set Top = Top - 1
// Now lets move the replaced projectile.
call arProj<i>.tick()
endif
else
// Projectile is alive, lets work with it.
call arProj<i>.tick()
endif
set i = i - 1
endloop
endfunction
private function Unit2Projectile takes unit u, real nvx, real nvy returns nothing
//set Top = Top + 1
//set arProj[Top] = Projectile.create()
//set arProj[Top].Unit = u
//set arProj[Top].vx = nvx
//set arProj[Top].vy = nvy
endfunction
function CreateProjectile takes player p returns Projectile
set Top = Top + 1
set arProj[Top] = Projectile.altCreate(p)
return arProj[Top]
endfunction
private function Test takes nothing returns nothing
local Projectile p = CreateProjectile(Player(0))
call p.setModel("Abilities\\Weapons\\Mortar\\MortarMissile.mdl")
set p.bearing = GetRandomReal(0,360)
set p.velocity = GetRandomReal(0,15)
endfunction
private function Init takes nothing returns nothing
debug call TimerStart(CreateTimer(), 1, true, function Test)
set Timer = CreateTimer()
call TimerStart(Timer, 0.01, true, function Update)
endfunction
endlibrary</i></i></i></i>
Alright, my first round of questions.
1). How do I check if a struct has been allocated? You can see I am checking if the unit is null to see if the struct has been destroyed, but I'm sure there is a proper way.