- Joined
- Jan 11, 2009
- Messages
- 3,414
Hi.
I just made a turret system for the ships in WiF, which will use dummy turret units with positions automatically adjusted to their position on the ship every 0.033 seconds. However, i am experiencing a lot of trouble with this.
here is the script:
and here is the script for attaching turrets to the units:
Second, despite that the calculation for the polar coordinate seems correct, the turrets seem to end up slightly to the side of the model instead of the center, and just feels generallt illusive.
Third and last, when i move hte unit, i must also change the facing of the turret to match the rotation of the ship itself. My intention was for the turret to have the same relative angle towards the ship when the ship rotates, but instead, the turrets will face a set angle and spin around towards this angle as the ship moves.
What i would like help to achive is this: If the turret is attacking, it will ofcourse be facing the target. When no enemies are nearby, it will be facing forwards (in the same angle as the ship). When attacking, the turret will turn around by itself, but how do i make it so that it always faces forwards otherwise?
Any help greately appreciated.
I just made a turret system for the ships in WiF, which will use dummy turret units with positions automatically adjusted to their position on the ship every 0.033 seconds. However, i am experiencing a lot of trouble with this.
here is the script:
JASS:
library Ship requires ListModule
globals
private constant real TIMER_INTERVAL = 0.045
endglobals
struct turret
implement List
real z
real ang
real dist
unit turret
unit ship
private static location height
private static timer interval
private static method setTurrets takes nothing returns nothing
local thistype this = .first
local real x
local real y
local real a
local real z
loop
exitwhen this == 0
call MoveLocation(.height, GetUnitX(.turret), GetUnitY(.turret))
set z = GetLocationZ(.height) //At first, i used this to obtain the corrected height of the unit, but it turned out wrong. Explanation below.
call ClearTextMessages() //just debug
call BJDebugMsg(R2S(GetUnitFlyHeight(.turret)))
set a = ModuloReal(GetUnitFacing(.ship)+.ang, 360)
set x = GetUnitX(.ship) + .dist * Cos(a * bj_DEGTORAD)
set y = GetUnitY(.ship) + .dist * Sin(a * bj_DEGTORAD)
set a = ModuloReal(GetUnitFacing(.ship)-GetUnitFacing(.turret), 360)
call SetUnitX(.turret, x)
call SetUnitY(.turret, y)
call SetUnitFacing(.turret, a)
call SetUnitFlyHeight(.turret, .z, 0)
if .count < 1 then
call PauseTimer(.interval)
endif
set this = .next
endloop
endmethod
static method create takes integer unitid, real x, real y, real z, real ang, real faceang, real dist, unit ship returns turret
local thistype this = thistype.allocate()
local unit u = CreateUnit(GetOwningPlayer(ship), unitid, x, y, ModuloReal(faceang+GetUnitFacing(ship), 360))
call UnitAddAbility(u, 'Aloc')
call .listAdd()
set .ang = ang
set .dist = dist
set .ship = ship
set .turret = u
set .z = z
if .count == 1 then
call TimerStart(.interval, TIMER_INTERVAL, true, function thistype.setTurrets)
endif
return this
endmethod
private method onDestroy takes nothing returns nothing
call .listRemove()
set .ship = null //While we're at it, i don't actually need to null theese, do i? I mean, they are technically inside a global array?
set .turret = null
endmethod
private static method onInit takes nothing returns nothing
set .interval = CreateTimer()
set .height = Location(0, 0)
endmethod
endstruct
endlibrary
and here is the script for attaching turrets to the units:
-
Turret Spawn
-
Events
- Unit - A unit enters (Playable map area)
- Conditions
-
Actions
-
Custom script: local turret T
-
Multiple FunctionsIf (All Conditions are True) then do (Then Actions) else do (Else Actions)
-
If - Conditions
- (Unit-type of (Triggering unit)) Equal to (==) Cruiser (Graf Spee)
-
Then - Actions
- Custom script: set T = turret.create( 'h01I', GetUnitX(GetTriggerUnit()), GetUnitY(GetTriggerUnit()), 33, 0, 0, 120, GetTriggerUnit() )
- Custom script: set T = turret.create( 'h01I', GetUnitX(GetTriggerUnit()), GetUnitY(GetTriggerUnit()), 33, 180, 0, 120, GetTriggerUnit() )
- Else - Actions
-
If - Conditions
-
Multiple FunctionsIf (All Conditions are True) then do (Then Actions) else do (Else Actions)
-
Custom script: local turret T
-
Events
Second, despite that the calculation for the polar coordinate seems correct, the turrets seem to end up slightly to the side of the model instead of the center, and just feels generallt illusive.
Third and last, when i move hte unit, i must also change the facing of the turret to match the rotation of the ship itself. My intention was for the turret to have the same relative angle towards the ship when the ship rotates, but instead, the turrets will face a set angle and spin around towards this angle as the ship moves.
What i would like help to achive is this: If the turret is attacking, it will ofcourse be facing the target. When no enemies are nearby, it will be facing forwards (in the same angle as the ship). When attacking, the turret will turn around by itself, but how do i make it so that it always faces forwards otherwise?
Any help greately appreciated.