You can use this fpscontroller2.lua from Editor or by dragging it over Engine.exe.
--stuff which should be part of LE
--the best working round function, works also for negative idp (written by Robert Jay Gould)
function round(num, idp)
return tonumber(string.format("%." .. (idp or 0) .. "f", num))
end
BLEND_NONE=0
BLEND_ALPHA=1
--end of stuff which should be part of LE
if fw==nil then --we are not in Editor
RegisterAbstractPath("")
Graphics(800,600)
fw=CreateFramework()
scene=LoadScene("abstract::tunnels.sbx")
scene:SetCollisionType(COLLISION_SCENE)
TFilter(1)
AFilter(4)
standalone=1
end
require("Scripts/constants/collision_const")
require("Scripts/constants/engine_const")
require("Scripts/LinkedList")
require("Scripts/filesystem")
require("Scripts/math/math")
require("scripts/classes/bullet")
--Variables
dx=0.0
dy=0.0
camerapitch=0.0
camerayaw=0.0
move=0.0
strafe=0.0
--Create a player controller
controller=CreateController(1.8,0.45,0.25,45)
controller:SetCollisionType(COLLISION_CHARACTER,0)
controller:SetMass(10)
if standalone==1 then
controller:SetPosition(Vec3(38.8,5.7,7))
else
controller:SetPosition(fw.main.camera.position)
end
camerapitch=fw.main.camera.rotation.x
camerayaw=fw.main.camera.rotation.y+180
local gunscale=0.6
local vwep = LoadMesh("abstract::vwep_hands.gmf")
LoadMesh("abstract::vwep_gun.gmf",vwep)
vwep:SetParent(fw.main.camera,0)
vwep:SetPosition(Vec3(-0.18*gunscale,-0.03*gunscale,0.37*gunscale),0)
vwep:SetScale(Vec3(0.04*gunscale,0.04*gunscale,0.04*gunscale))
local gundisplayposition = vwep:GetPosition()
sound_gunshot = LoadSound("abstract::gunshot.ogg")
source_gunshot = CreateSource(sound_gunshot)
source_gunshot:SetVolume(0.5)
vwep :SetShadowMode(0,1)
local displayposition=Vec3(-0.26/2.0,-0.03,0.19)
local muzzleflash = CreatePointLight(3)
muzzleflash:SetParent( vwep )
muzzleflash:SetColor(Vec4(1,0.6,0.0,1.0))
muzzleflash:SetPosition( displayposition )
muzzleflash:SetShadowMode(0)
HideMouse()
MoveMouse(GraphicsWidth()/2,GraphicsHeight()/2)
FlushKeys()
FlushMouse()
local pick
local camera = fw.main.camera
local remainingtime
local starttime=AppTime()
local gameduration=2--length of game in minutes
local gamemode=0
gunpos = vwep.position:Copy()
local smoothedgunswayamplitude=0.0
local smoothedgunswayspeed =0.0
local guntime = 0.0
local recoil = 0.0
local lastfiretime=0.0
local smoothrecoil=0.0
local swaydamping=0.0
local smoothswaydamping=0.0
local lightsmoothing =0.0
local gunlight = 0.0
--Flashlight
flashlight = {}
flashlight.light = CreateSpotLight(8)
flashlight.light:Hide()
flashlight.sound_switch = LoadSound("abstract::switch.wav")
flashlight.state=0
flashlight.light:SetConeAngles(30,35)
flashlight.light:SetRotation(Vec3(5,0,0))
flashlight.light:SetShadowmapSize(512)
flashlight.light:Paint(LoadMaterial("abstract::flashlight.mat"))
function flashlight:SetState( state )
if state~=self.state then
self.state=state
if state==0 then
self.light:Hide()
else
self.light:Show()
end
if self.sound_switch~=nil then
self.sound_switch:Play()
end
end
end
function ShootBullet( position, direction )
-- local speed=100.0
-- local pick = LinePick( position, Vec3(position.x+direction.x * speed) )
end
function DrawHUD(contr)
SetBlend(BLEND_ALPHA)
DrawText("pos: "..round(contr.position.x,3)..","..round(contr.position.y,3)..","..round(contr.position.z,3),1,FontHeight()*5)
DrawText("rot: "..round(contr.rotation.x,3)..","..round(contr.rotation.y,3)..","..round(contr.rotation.z,3),1,FontHeight()*6)
SetBlend(BLEND_NONE)
end
--main function
while KeyHit(KEY_ESCAPE)==0 do
jump=KeyHit(KEY_SPACE)*6.0
if controller:IsAirborne()==1 then jump=0 end
local time = AppTime()/3200.0
local frame = time*(179.0-96.0)+96.0
frame=Clamp( frame, 96, 179 )
vwep:Animate(96,1,0,1)
--Camera look
gx=Round(GraphicsWidth()/2)
gy=Round(GraphicsHeight()/2)
dx=Curve((MouseX()-gx)/4.0,dx,3.0/AppSpeed())
dy=Curve((MouseY()-gy)/4.0,dy,3.0/AppSpeed())
MoveMouse(gx,gy)
camerapitch=camerapitch+dy
camerayaw=camerayaw-dx
camerapitch=math.min(camerapitch,90)
camerapitch=math.max(camerapitch,-90)
fw.main.camera:SetRotationf(camerapitch,camerayaw,0,1)
movespeed=6
movesmoothing=10
if controller:IsAirborne()==1 then
movesmoothing=200
end
--Player movement
move=Curve( (KeyDown(KEY_W)-KeyDown(KEY_S))*movespeed,move,movesmoothing)
strafe=Curve( (KeyDown(KEY_D)-KeyDown(KEY_A))*movespeed,strafe,movesmoothing)
--Use objects
if KeyHit(KEY_E)==1 then
pick=CameraPick(camera,Vec3(GraphicsWidth()/2,GraphicsHeight()/2,2.0),0,0)
if pick~=nil then
repeat
if pick.entity:GetClass()==ENTITY_MODEL then
break
end
pick.entity=pick.entity.parent
until pick.entity==nil
if pick.entity~=nil then
pick.entity:SendMessage("use",controller,0)
end
end
end
--Update controller
controller:Update(camerayaw,move,strafe,jump,40,10)
fw:Update()
if KeyHit(KEY_F)==1 then
flashlight:SetState(1-flashlight.state)
end
--Position camera
camera:SetPositionf(controller.position.x,controller.position.y+0.8,controller.position.z,1)
time=AppTime()
gunfirefrequency=80
gunswayspeed=0.001*20.0
gunoffset = gunpos:Copy()
gunswayamplitude = 0.02
if KeyDown(KEY_W)==1 or KeyDown(KEY_D)==1 or KeyDown(KEY_A)==1 or KeyDown(KEY_S)==1 then
gunswayamplitude = 0.03
gunswayspeed=0.005*20.0
end
smoothedgunswayamplitude = Curve( gunswayamplitude, smoothedgunswayamplitude,8.0 )
smoothedgunswayspeed = Curve( gunswayspeed, smoothedgunswayspeed,8.0 )
if smoothrecoil<0.001 then
guntime = guntime + AppSpeed() * smoothedgunswayspeed * math.max(0.0,1.0 - smoothswaydamping)
end
gunoffset.z = gunoffset.z - smoothrecoil * 0.05
--smoothedgunswayamplitude = smoothedgunswayamplitude * (1.0 - smoothswaydamping)
gunoffset.x = gunoffset.x + math.sin( guntime ) * smoothedgunswayamplitude * gunscale
gunoffset.y = gunoffset.y + (1.0-math.cos( guntime*2.0 )) * 0.005 * gunscale-- * math.min(1.0,1.0 - smoothswaydamping))
vwep:SetPosition( gunoffset )
recoil = recoil-0.1
swaydamping = math.max( swaydamping - 0.05, 0.0 )
recoil = math.max(recoil,0.0)
smoothrecoil=Curve(recoil,smoothrecoil,3.0)
smoothswaydamping = Inc( swaydamping ,smoothswaydamping,0.01 )
gunlight = math.max( gunlight- 0.2, 0.0 )
lightsmoothing =gunlight-- Curve(gunlight,lightsmoothing,8.0)
muzzleflash:SetColor(Vec4(1.0*lightsmoothing,0.6*lightsmoothing,0.0,1.0))
if lightsmoothing <0.01 then
muzzleflash:Hide()
end
if MouseDown(1)==1 then
if AppTime()-lastfiretime>gunfirefrequency then
recoil = 1.0
lastfiretime=AppTime()+math.random(0,20)
gunswayspeed=0.0
gunlight = 1.0
source_gunshot:Play()
source_gunshot:SetPitch(1.0 + (math.random()-0.5)*0.05 )
swaydamping = 1.0
muzzleflash:Show()
CreateBullet(vwep:GetPosition(1) , fw.main.camera.mat:K():Scale(300))
end
end
UpdateBullets()
flashlight.light:SetPosition(fw.main.camera:GetPosition(1))
flashlight.light:SetRotationf( CurveAngle( fw.main.camera.rotation.x, flashlight.light.rotation.x, 3.0/AppSpeed() ), CurveAngle( fw.main.camera.rotation.y, flashlight.light.rotation.y, 3.0/AppSpeed() ) )
flashlight.light:Movef(-0.07,-0.04,0.02)
fw:Render()
DrawHUD(controller)
Flip(0)
end
controller:Free()
vwep:Free()
ShowMouse()