3rdPerson.lua
Script.Camera = nil --entity
Script.positionDifference = Vec2(0,0)
Script.picksphere = Model:Sphere()
function Script:Start()
self.playerMovement = Vec3(0,0,0)
self.cameraYaw = 0
self.cameraPitch = 10
self.dx = 0
self.dy = 0
self.mousePos = 100
self.moveSpeed = 10
self.entity:SetKeyValue("type", "player")
-- HUD stuff
self.crosshairs = Texture:Load("Materials/HUD/crosshair.tex")
self.entity:SetFriction(0,0)
local material = Material:Create()
material:SetBlendMode(5)--Blend.Invisible
self.entity:SetMaterial(material)
material:Release()
self.entity:SetShadowMode(0)
self.picksphere:SetColor(1.0,0.0,0.0)
self.picksphere:SetPickMode(0)
end
function Script:UpdateWorld()
end
function Script:CameraRotation()
-- rotate yaw and tell the player about it so the character can use it in SetInput()
local mpos = window:GetMousePosition()
self.dx = Math:Curve((mpos.x - self.mousePos) / 10.0, self.dx, 3.0 / Time:GetSpeed())
self.cameraYaw = self.cameraYaw + self.dx
-- rotate pitch
self.dy = Math:Curve((mpos.y - self.mousePos) / 10.0, self.dy, 3.0 / Time:GetSpeed())
local pitch = self.cameraPitch + self.dy
if pitch > -25 and pitch < 55 then
self.cameraPitch = pitch
end
-- reset the mouse position
window:SetMousePosition(self.mousePos, self.mousePos)
end
function Script:Movement()
-- handle player movement
self.playerMovement.z = ((window:KeyDown(Key.W) and 1 or 0) - (window:KeyDown(Key.S)and 1 or 0)) * Time:GetSpeed() * self.moveSpeed
self.playerMovement.x = ((window:KeyDown(Key.D) and 1 or 0) - (window:KeyDown(Key.A)and 1 or 0)) * Time:GetSpeed() * self.moveSpeed
-- reduce speed when moving backwards
if self.playerMovement.z < 0 then
self.playerMovement.z = self.playerMovement.z * .50
end
local positionPlayer = self.entity:GetPosition(false)
self.positionDifference.y = Math:Curve(positionPlayer.y ,self.positionDifference.y,3)
-- move the camera to the player and set the yaw from 3rd person view
self.Camera:SetPosition(self.entity:GetPosition(true), true)
self.Camera:SetRotation(Vec3(self.cameraPitch, self.cameraYaw, 0), true)
-- offset the camera up and back
self.Camera:Move(0, 3, -4)
-- rotate the model along with the camera
self.entity:SetRotation(Vec3(0, self.cameraYaw, 0))
-- move the controller in the rotation of the player
self.entity:SetInput(self.cameraYaw, self.playerMovement.z,self.playerMovement.x , 0, false, 1)
end
function Script:Fire()
-- hide ourselves do we don't accidently pick ourselves
self.entity:Hide()
local p = PickInfo()
local c = Vec2(0)
c.x = window:GetWidth() / 2
c.y = window:GetHeight() / 2
if self.Camera:Pick(c.x, c.y, p) then
if p.entity ~= nil then
self.picksphere:SetPosition(p.position,false)
end
end
self.entity:Show()
end
function Script:UpdatePhysics()
self:Movement()
self:CameraRotation()
if window:MouseDown(1) == true then
self:Fire()
end
end
function Script:PostRender(context)
local width = window:GetWidth()
local height = window:GetHeight()
local w = self.crosshairs:GetWidth()
local h = self.crosshairs:GetHeight()
local pos = Vec2(0)
pos.x = (width / 2) - (w / 2)
pos.y = (height / 2) - (h / 2)
context:SetBlendMode(Blend.Alpha)
context:DrawImage(self.crosshairs, pos.x, pos.y)
context:DrawText("Mouse button Left to fire", 10, 70)
context:DrawText("W/S to walk forward / backward", 10, 90)
context:DrawText("A/D to strafe", 10, 110)
context:SetBlendMode(Blend.Solid)
end