- Joined
- Oct 19, 2014
- Messages
- 187
DRIVE ENGINE SYSTEM
Credits
-Nestharus
-The_Witcher
Requires
-World Bounds
-Keyboard System
-Cam System
Changelogs
v1.0.0
-release
JASS:
library DriveEngine /* v1.0.0 bj JC Helas*/ initializer init /*
**************************************************************
*
* System is only MPI, allow players to control a
* unit that will move like a running car.
* Also player can switch to 3D view or Person view by
* pressing ESC.
*
**************************************************************
*
* ********************
* */ requires /*
* */ KeyboardSystem,/*
* */ WorldBounds /*
* ********************
*
* API: call DriveEngine_AddUnit(player owner,unit car,real speedmax,real speed accellerate,real handling,real break,real weight,real length,integer animation)
*
*
*************************************************************/
globals
public constant real DRIFT = 1.50
public constant real FRICTION = 0.50
private constant real PERIODIC = 0.031250
private constant real FRAME = 1.0/PERIODIC
private location locz=Location(0,0)
endglobals
public struct data
public unit u
public real speedmax
public real speedaccel
public real speedpresent
public real handling
public real break
public real weight
public real length
public boolean running
public boolean falling
public boolean thirdperson
integer move
real zfly
real zangle
real angle
endstruct
private function GetLocZ takes real x,real y returns real
call MoveLocation(locz,x,y)
return GetLocationZ(locz)
endfunction
private function GetZAngle takes unit u,real length returns real
local real x=GetUnitX(u)
local real y=GetUnitY(u)
local real a=GetUnitFacing(u)
local real cos=Cos(a*bj_DEGTORAD)
local real sin=Sin(a*bj_DEGTORAD)
local real x1=x+length*cos
local real x2=x-length*cos
local real y1=y+length*sin
local real y2=y-length*sin
return GetLocZ(x2,y2)-GetLocZ(x1,y1)
endfunction
private function Loop takes nothing returns nothing
local data this=0
local boolean up
local boolean down
local boolean left
local boolean right
local player p
local real a=0.0
local real za=0.0
local real x
local real y
loop
exitwhen this==13
set p=Player(this)
if GetPlayerSlotState(p)==PLAYER_SLOT_STATE_PLAYING and this.u!=null then
set up=IsKeyDown(KEY_UP,p)
set down=IsKeyDown(KEY_DOWN,p)
set left=IsKeyDown(KEY_LEFT,p)
set right=IsKeyDown(KEY_RIGHT,p)
set x=GetUnitX(this.u)
set y=GetUnitY(this.u)
call SetUnitTimeScale(this.u,(1.0/10.0)*this.speedpresent)
if not this.falling then
if down then
set a=-(this.speedmax/4.0)
if this.speedpresent>a then
if this.speedpresent>this.break then
set this.speedpresent=this.speedpresent-(((FRICTION+this.break)*((1.0/50.0)*this.speedpresent)))
else
set this.speedpresent=this.speedpresent-this.break
endif
else
set this.speedpresent=a
endif
set a=0.0
elseif up then
if this.speedpresent<this.speedmax then
set this.speedpresent=this.speedpresent+this.speedaccel
endif
endif
endif
if this.speedpresent!=0.0 then
set za=GetZAngle(this.u,this.length)
if za>-10.0 and za<10.0 then
if not this.falling and this.speedpresent>20.0 then
set za=za*-1
if za>this.zangle then
set this.zangle=za
elseif this.zangle>0.0 and za<=0.0 then
set this.falling=true
set this.zfly=GetLocZ(x,y)
call UnitAddAbility(this.u,'Amrf')
call UnitRemoveAbility(this.u,'Amrf')
endif
set za=za*-1
endif
if not up and not down then
if za==0.0 then
if this.speedpresent>0.0 then
set this.speedpresent=this.speedpresent-((FRICTION*(1.0-((1.0/50.0)*this.speedpresent))))
if this.speedpresent<0.0 then
set this.speedpresent=0.0
endif
else
set this.speedpresent=this.speedpresent+FRICTION
if this.speedpresent>0.0 then
set this.speedpresent=0.0
endif
endif
else
if (za<0.0 and this.speedpresent>-50.0) or (za>0.0 and this.speedpresent<50.0) then
set this.speedpresent=this.speedpresent+(za/FRAME)
endif
endif
endif
else
set this.speedpresent=za/3.0
endif
if not this.falling then
set a=GetUnitFacing(this.u)
if left or right or a!=this.angle then
if left then
set a=a+this.handling
elseif right then
set a=a-this.handling
endif
if a>360.0 then
set a=a-360.0
elseif a<0.0 then
set a=360.0-(a*-1.0)
endif
call SetUnitFacing(this.u,a)
if a!=this.angle then
if (a<90.0 and this.angle>270.0) then
set a=a+360.0
elseif(this.angle<90.0 and a>270.0) then
set a=a-360.0
endif
if this.speedpresent!=0.0 then
set a=a-this.angle
set this.angle=this.angle+(a*(1.0-((1.0/(this.speedmax+DRIFT))*this.speedpresent)))
if this.angle>360.0 then
set this.angle=this.angle-360.0
elseif this.angle<0.0 then
set this.angle=360.0-(this.angle*-1.0)
endif
endif
endif
endif
else
call SetUnitFlyHeight(this.u,this.zfly-GetLocZ(x,y),0.0)
set this.zangle=this.zangle+(this.speedpresent/2.0)
set this.zfly=this.zfly-(this.weight+(this.zangle/FRAME))
if this.zfly<=0.0 then
set this.falling=false
set this.zangle=0.0
endif
endif
endif
if not this.running and this.speedpresent!=0.0 then
call SetUnitAnimationByIndex(this.u,this.move)
set this.running=true
elseif this.speedpresent==0.0 and this.running then
call SetUnitAnimationByIndex(this.u,1)
set this.running=false
endif
set x=x+this.speedpresent*Cos(this.angle*bj_DEGTORAD)
set y=y+this.speedpresent*Sin(this.angle*bj_DEGTORAD)
if x<WorldBounds.maxX and x>WorldBounds.minX and y<WorldBounds.maxY and y>WorldBounds.minY then
call SetUnitX(this.u,x)
call SetUnitY(this.u,y)
endif
endif
set this=this+1
endloop
endfunction
public function AddUnit takes player p,unit u,real sm,real sa,real h,real b,real weight,real length,integer move returns nothing
local data this=GetPlayerId(p)
set this.u=u
set this.speedmax=R2I(sm/FRAME)
set this.speedaccel=sa/FRAME
set this.speedpresent=0.0
set this.handling=h
set this.break=b/FRAME
set this.weight=R2I(weight/FRAME)
set this.length=R2I(length/FRAME)
set this.zfly=0.0
set this.zangle=0.0
set this.angle=GetUnitFacing(u)
set this.running=false
set this.falling=false
set this.thirdperson=false
set this.move=move
call SetCameraTargetControllerNoZForPlayer(p,u,0,0,false)
endfunction
private function ChangeCam takes nothing returns boolean
local player p=GetTriggerPlayer()
local data this=GetPlayerId(p)
if this.u!=null then
if this.thirdperson then
call ReleaseCameraUnit(p)
call SetCameraTargetControllerNoZForPlayer(p,this.u,0,0,false)
set this.thirdperson=false
else
call SetCameraUnit(this.u,p)
set this.thirdperson=true
endif
endif
return false
endfunction
private function init takes nothing returns nothing
local data i=0
set gg_trg_DE_System=CreateTrigger()
loop
exitwhen i==13
set i.u=null
call TriggerRegisterPlayerEvent(gg_trg_DE_System,Player(i), EVENT_PLAYER_END_CINEMATIC)
set i=i+1
endloop
call TriggerAddCondition(gg_trg_DE_System,Filter(function ChangeCam))
call TimerStart(CreateTimer(),PERIODIC,true,function Loop)
endfunction
endlibrary
Attachments
Last edited: