Jump to content

fpcontroller revisited


gordonramp
 Share

Recommended Posts

As the Framework is now accessible with Lua outside of the editor, I've revisited my fpcontroller code.. I can load a scene and look around with mouselook but am unable to move.. can someone tell me why..

 

fpcontroller revisited

require("Scripts/constants/collision_const")
require("Scripts/constants/engine_const")
require("Scripts/LinkedList")
require("Scripts/filesystem")
require("Scripts/math/math")

--Register abstract path
RegisterAbstractPath("")

--Set graphics mode
if Graphics(1024,768)==0 then
Notify("Failed to set graphics mode.",1)
return
end

--Create framewerk object and set it to a global object so other scripts can access it
fw=CreateFramework()
if fw==nil then
Notify("Failed to initialize engine.",1)
return
end
SetGlobalObject("framewerk",fw)

camera=fw.main.camera
camera:SetPositionf(-4,1.2,-4)

--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:SetPosition(fw.main.camera.position)
camerapitch=fw.main.camera.rotation.x
camerayaw=fw.main.camera.rotation.y
controller:Move(Vec3(0,-0.9,0))

--HideEntity(vwep)
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

scene=LoadScene("abstract::tunnels.sbx")

light=CreateDirectionalLight()
light:SetRotationf(45,45,45)

while KeyHit(KEY_ESCAPE)==0 do
jump=KeyHit(KEY_SPACE)*6.0
if controller:IsAirborne()==1 then jump=0 end

--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,-89.99)
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)

--Update controller
controller:Update(camerayaw,move,strafe,jump,40,10)

if KeyHit(KEY_ESCAPE)==1 then break end

fw:Update()
fw:Render()
Flip(0)

end

AMD Athlon x2 7750 2.7ghz, 6gb ddr2 ram, Galaxy9800GT 1gig ddr2 video card, Windows 7,64.

Link to comment
Share on other sites

With some adjustments and input from Lumooja's fpcontroller code, this now works in the Editor and outside the Editor.. No Gun but has Flashlight.

 

 

function round(num, idp)        
return tonumber(string.format("%." .. (idp or 0) .. "f", num))
end
BLEND_NONE=0
BLEND_ALPHA=1
--
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")

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(42.0,1,-10))
else        
controller:SetPosition(fw.main.camera.position)
end
camerapitch=fw.main.camera.rotation.x
camerayaw=fw.main.camera.rotation.y-90
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

--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

--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 )        
--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)        


--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()        


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()        

Flip(0)
end
controller:Free()

ShowMouse()

AMD Athlon x2 7750 2.7ghz, 6gb ddr2 ram, Galaxy9800GT 1gig ddr2 video card, Windows 7,64.

Link to comment
Share on other sites

I'm glad you brought that up. What I find is that code in code tags on this forum doesn't copy and paste with wordwrap ie. it pastes as one long line and takes forever to format, (please explain how to do it if I'm incorrect). So I figure I'm making life easier for people by putting my code in quotes which does paste with wordwrap (formatiing). Also, indentation is not a requirement of coding, it is only a preference by some.

AMD Athlon x2 7750 2.7ghz, 6gb ddr2 ram, Galaxy9800GT 1gig ddr2 video card, Windows 7,64.

Link to comment
Share on other sites

I'm glad you brought that up. What I find is that code in code tags doesn't copy and paste with wordwrap ie. it pastes as one long line and takes forever to format, (please explain how to do it if I'm incorrect). So I figure I'm making life easier for people by putting my code in quotes which does paste with wordwrap (formatiing). Also, indentation is not a requirement of coding, it is only a preference by some.

 

what are you using for a browser?

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

Good point, Internet Explorer 7.

I just downloaded Google Chrome and copy and paste between code tags formats with it. So I guess I'll be using Google Chrome in the future. Thanks. However don't expect my code to be always indented, I actually prefer it nice and neat against the left hand margin. Easier to read. :)

AMD Athlon x2 7750 2.7ghz, 6gb ddr2 ram, Galaxy9800GT 1gig ddr2 video card, Windows 7,64.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...