Dirac
22710180
- Reaction score
- 147
Hello guys i just made this trigger which purpose is to move a ship through water using the arrow keys. I've never used structs nor vJass before. I just want to know if theres a way to enhance my code.
- I tried to clear all BJs
- I need a better way to initialize my global arrays
- Is there a way to change global arrays from other triggers?
- What does a static method do?
- What is a keyword?
+rep will be rewarded for those who help me improve my coding skills
- I tried to clear all BJs
- I need a better way to initialize my global arrays
- Is there a way to change global arrays from other triggers?
- What does a static method do?
- What is a keyword?
+rep will be rewarded for those who help me improve my coding skills
JASS:
scope Movement initializer OnInit
globals
private unit array ship
private boolean array left
private boolean array right
private boolean array up
public real array speed
public real array angle
public real array maxturnangle
public real array acceleration
endglobals
private struct shipdata
unit u
integer i
real x
real y
method store takes unit u, integer i returns nothing
set this.u = u
set this.i = i
endmethod
method acc takes real a, boolean b,real c returns nothing
if this.x<c and b== true then
set this.x = this.x+a
elseif this.x > 0 then
set this.x = this.x-a
endif
endmethod
method turn takes real a,boolean b,boolean c, real d returns nothing
if b != c then
if b==true and this.y < d then
set this.y = this.y + a
elseif c==true and this.y > d*(-1) then
set this.y = this.y - a
endif
else
if this.y > 0 then
set this.y = this.y - a
elseif this.y < 0 then
set this.y = this.y + a
endif
endif
endmethod
endstruct
private function setglobals takes nothing returns nothing
local shipdata ud=shipdata.create()
set ship[0] = CreateUnit(Player(0),039;H000039;,-3000.,-3000.,0.)
call SetUnitUserData(ship[0],ud)
call ud.store(ship[0],GetPlayerId(GetOwningPlayer(ship[0])))
endfunction
private function pressl takes nothing returns nothing
if left[GetPlayerId(GetTriggerPlayer())] == true then
set left[GetPlayerId(GetTriggerPlayer())] = false
else
set left[GetPlayerId(GetTriggerPlayer())] = true
endif
endfunction
private function pressr takes nothing returns nothing
if right[GetPlayerId(GetTriggerPlayer())] == true then
set right[GetPlayerId(GetTriggerPlayer())] = false
else
set right[GetPlayerId(GetTriggerPlayer())] = true
endif
endfunction
private function pressu takes nothing returns nothing
if up[GetPlayerId(GetTriggerPlayer())] == true then
set up[GetPlayerId(GetTriggerPlayer())] = false
else
set up[GetPlayerId(GetTriggerPlayer())] = true
endif
endfunction
public function periodic takes nothing returns nothing
local real x
local real y
local integer i = 0
local shipdata ud=GetUnitUserData(ship<i>)
loop
exitwhen i == 1
call ud.turn (0.05,left<i>,right<i>,maxturnangle<i>)
call ud.acc (acceleration<i>, up<i>, speed<i>)
set x = GetUnitX(ship<i>) + ud.x*Cos(Deg2Rad(GetUnitFacing(ship<i>)+ud.y))
set y = GetUnitY(ship<i>) + ud.x*Sin(Deg2Rad(GetUnitFacing(ship<i>)+ud.y))
call SetUnitFacing(ship<i>,GetUnitFacing(ship<i>)+ud.y)
call SetUnitX(ship<i>,x)
call SetUnitY(ship<i>,y)
if GetLocalPlayer() != Player(i) then
set x=0.
set y=0.
endif
call PanCameraToTimed(x,y,0.)
call SetCameraRotateMode(x,y,Deg2Rad(270+GetUnitFacing(ship<i>)),0.)
call SetCameraField(CAMERA_FIELD_TARGET_DISTANCE,3900.,0.)
set i = i+1
endloop
endfunction
// =============================================================
private function OnInit takes nothing returns nothing
local trigger mov = CreateTrigger() //create a local for each trigger
local trigger left = CreateTrigger()
local trigger right = CreateTrigger()
local trigger up = CreateTrigger()
local trigger init = CreateTrigger()
local trigger t = CreateTrigger()
local integer i = 0
loop
exitwhen i == 5
call TriggerRegisterPlayerEvent (left, Player (i), EVENT_PLAYER_ARROW_LEFT_DOWN) // add the arrow key events to the triggers
call TriggerRegisterPlayerEvent (left, Player(i), EVENT_PLAYER_ARROW_LEFT_UP)
call TriggerRegisterPlayerEvent (right, Player(i), EVENT_PLAYER_ARROW_RIGHT_DOWN)
call TriggerRegisterPlayerEvent (right, Player(i), EVENT_PLAYER_ARROW_RIGHT_UP)
call TriggerRegisterPlayerEvent (up, Player(i), EVENT_PLAYER_ARROW_UP_DOWN)
call TriggerRegisterPlayerEvent (up, Player(i), EVENT_PLAYER_ARROW_UP_UP)
set i = i+1
endloop
call TriggerRegisterTimerEvent(t,0.0325,true)
call TriggerAddAction(t, function periodic )
call TriggerRegisterTimerEvent(init,0,false)
call TriggerAddAction( left, function pressl )
call TriggerAddAction( right, function pressr )
call TriggerAddAction( up, function pressu )
call TriggerAddAction(init, function setglobals)
endfunction
endscope</i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i>