Difference between revisions of "Camera Scripts"

From X-Moto
Jump to: navigation, search
 
(Extended Camera, Static Camera, Zone Camera (sprites based))
Line 1: Line 1:
'''Extended Camera'''
+
== Extended Camera ==
  
-- ExtendedCamera
+
This script adds new functions to control camera position.
do
+
 
 +
'''Game.CameraPos()''' returns camera position (x, y) relative to player position.
 +
 
 +
'''Usage:''' x, y = Game.CameraPos()
 +
 
 +
'''Game.CameraPosAbs()''' returns camera position (x, y) in level coordinates.
 +
 
 +
'''Usage:''' x, y = Game.CameraPosAbs()
 +
 
 +
'''Game.CameraMoveTo(x, y)''' moves the camera to position (x, y) relative to player.
 +
 
 +
'''Example:''' Game.CameraMoveTo(0, 0) -- return camera to its normal state
 +
 
 +
'''Game.CameraMoveAbs(x, y)''' moves the camera to position (x, y) in level coordinates.
 +
 
 +
'''Example:''' Game.CameraMoveTo(Game.GetEntityPos("cam1")) -- make camera look at "cam1" sprite
 +
 
 +
To use the functions add this script to your Lua file:
 +
 
 +
-- ExtendedCamera
 +
do
 
   local CamX, CamY = 0, 0
 
   local CamX, CamY = 0, 0
 
    
 
    
Line 12: Line 32:
 
     return x + CamX + 0.6, y + CamY
 
     return x + CamX + 0.6, y + CamY
 
   end
 
   end
 
+
 
   local Game_CameraMove = Game.CameraMove
 
   local Game_CameraMove = Game.CameraMove
 
   function Game.CameraMove(x, y, ...)
 
   function Game.CameraMove(x, y, ...)
Line 28: Line 48:
 
     return Game.CameraMove(x - px, y - py)
 
     return Game.CameraMove(x - px, y - py)
 
   end
 
   end
end
+
end
-- /ExtendedCamera
+
-- /ExtendedCamera
 +
 
 +
Author: GrayFace
 +
 
 +
== Static Camera ==
 +
 
 +
This script lets you set the camera to a fixed position. The camera will not follow the player any more.
 +
 
 +
'''StaticCamera(x, y)''' will set the camera to (x, y) coordinates in the level
 +
 
 +
'''StaticCamera()''' ''(without parameters)'' will reset the camera, so it will follow the player again
 +
 
 +
Example:
 +
function OnLoad()
 +
  StaticCamera(Game.GetEntityPos("cam1"))  -- make the camera always look at "cam1" sprite
 +
  return true
 +
end
 +
 
 +
To use Static Camera add '''Extended Camera''' script to your Lua file and then add this script:
 +
-- StaticCamera
 +
do
 +
  local camX, camY
 +
  local oldTick
 +
 
 +
  local function MyTick()
 +
    local ret = oldTick and oldTick() or true
 +
    Game.CameraMoveAbs(camX, camY)
 +
    return ret
 +
  end
 +
 +
  function StaticCamera(x, y)
 +
    if x then
 +
      if camX == nil then
 +
        oldTick = Tick
 +
        Tick = MyTick
 +
      end
 +
      camX, camY = x, y
 +
      Game.CameraMoveAbs(camX, camY)
 +
    else
 +
      if camX then
 +
        Tick = oldTick
 +
        camX, camY = nil, nil
 +
        Game.CameraMoveTo(0, 0)
 +
      end
 +
    end
 +
  end
 +
end
 +
-- /StaticCamera
 +
 
 +
Author: GrayFace
 +
 
 +
== Zone Camera (sprites based) ==
 +
 
 +
This script is based on idea from ''Cigam_Interesting Effect 03 (Zone Camera)'' level. It implements it using camera sprites instead of zones.
 +
 
 +
Usage:
 +
 
 +
Create sprites in your level at points where you want cameras to be. Name them "cam1", "cam2", "cam3" and so on. (use Effects -> X-moto -> Other -> Change Id in Inkscape) I suggest using invisible sprites.
 +
 
 +
Then call '''UseZoneCamera(cam_count, keep_cams)''' to turn zone cameras on.
 +
 
 +
'''cam_count''' parameter is the number of cameras.
 +
 
 +
If '''keep_cams''' parameter is ''true'', the sprites used as cameras will remain on the level. If it's ''false'' or not specified, the sprites will be deleted once zone camera is activated.
 +
 
 +
Example:
 +
function OnLoad()
 +
  UseZoneCamera(3)  -- if you have only 3 cameras: "cam1", "cam2", "cam3"
 +
  return true
 +
end
 +
 
 +
You can also make some camera preferable. Change its ''Sprite scale'' value to do so. (in Effects -> X-moto -> Entities -> Sprite dialog) Set bigger value to make the camera more 'respected'. The script likes bigger cameras ;)
 +
 
 +
Test your level in 1280x1024 resolution to make sure player is always visible.
 +
 
 +
To use Zone Camera add '''Extended Camera''' script to your Lua file and then add this script:
 +
 
 +
-- UseZoneCamera
 +
function UseZoneCamera(CamCount, keep_sprites)
 +
  -- const
 +
  local stick_radius = 0.5
 +
  local y_coeff = 0.8
 +
  -- /const
 +
 
 +
  local Cams = {}
 +
  local CurCam
 +
  local CamX, CamY = 0, 0
 +
 
 +
  local abs = math.abs
 +
  local sqrt = math.sqrt
 +
 
 +
  local function ChangeCam()
 +
    local x, y = Game.GetPlayerPosition()
 +
   
 +
    local function dist(cam)
 +
      local c = Cams[cam]
 +
      local dx, dy = abs(c[1] - x), abs(c[2] - y)*y_coeff
 +
      return sqrt(dx*dx + dy*dy)/c[3]
 +
    end
 +
   
 +
    local cam = CurCam
 +
    local m = cam and (dist(cam) - stick_radius)
 +
 
 +
    for i = 1, CamCount do
 +
      local d = dist(i)
 +
      if not m or d < m then
 +
        m = d
 +
        cam = i
 +
      end
 +
    end
 +
    CurCam = cam
 +
    Game.CameraMoveAbs(Cams[cam][1], Cams[cam][2])
 +
  end
 +
 
 +
  for i = 1, CamCount do
 +
    local name = "cam"..i
 +
    local cam = {Game.GetEntityPos(name)}
 +
    cam[3] = Game.GetEntityRadius(name)
 +
    Cams[i] = cam
 +
    if not keep_sprites then  Game.KillEntity(name)  end
 +
  end
 +
  ChangeCam()
 +
 
 +
  local oldTick = Tick
 +
  Tick = function()
 +
    local ret = oldTick and oldTick() or true
 +
    ChangeCam()
 +
    return ret
 +
  end
 +
end
 +
-- /UseZoneCamera
 +
 
 +
Author: GrayFace

Revision as of 05:23, 21 August 2009

Extended Camera

This script adds new functions to control camera position.

Game.CameraPos() returns camera position (x, y) relative to player position.

Usage: x, y = Game.CameraPos()

Game.CameraPosAbs() returns camera position (x, y) in level coordinates.

Usage: x, y = Game.CameraPosAbs()

Game.CameraMoveTo(x, y) moves the camera to position (x, y) relative to player.

Example: Game.CameraMoveTo(0, 0) -- return camera to its normal state

Game.CameraMoveAbs(x, y) moves the camera to position (x, y) in level coordinates.

Example: Game.CameraMoveTo(Game.GetEntityPos("cam1")) -- make camera look at "cam1" sprite

To use the functions add this script to your Lua file:

-- ExtendedCamera
do
 local CamX, CamY = 0, 0
 
 function Game.CameraPos()
   return CamX, CamY
 end
 function Game.CameraPosAbs()
   local x, y = Game.GetPlayerPosition()
   return x + CamX + 0.6, y + CamY
 end

 local Game_CameraMove = Game.CameraMove
 function Game.CameraMove(x, y, ...)
   if x ~= 0 and y ~= 0 then
     CamX = CamX + x
     CamY = CamY + y
     return Game_CameraMove(x, y, ...)
   end
 end
 function Game.CameraMoveTo(x, y)
   return Game.CameraMove(x - CamX, y - CamY)
 end
 function Game.CameraMoveAbs(x, y)
   local px, py = Game.CameraPosAbs()
   return Game.CameraMove(x - px, y - py)
 end
end
-- /ExtendedCamera

Author: GrayFace

Static Camera

This script lets you set the camera to a fixed position. The camera will not follow the player any more.

StaticCamera(x, y) will set the camera to (x, y) coordinates in the level

StaticCamera() (without parameters) will reset the camera, so it will follow the player again

Example:

function OnLoad()
 StaticCamera(Game.GetEntityPos("cam1"))  -- make the camera always look at "cam1" sprite
 return true
end

To use Static Camera add Extended Camera script to your Lua file and then add this script:

-- StaticCamera
do
 local camX, camY
 local oldTick
 
 local function MyTick()
   local ret = oldTick and oldTick() or true
   Game.CameraMoveAbs(camX, camY)
   return ret
 end

 function StaticCamera(x, y)
   if x then
     if camX == nil then
       oldTick = Tick
       Tick = MyTick
     end
     camX, camY = x, y
     Game.CameraMoveAbs(camX, camY)
   else
     if camX then
       Tick = oldTick
       camX, camY = nil, nil
       Game.CameraMoveTo(0, 0)
     end
   end
 end
end
-- /StaticCamera

Author: GrayFace

Zone Camera (sprites based)

This script is based on idea from Cigam_Interesting Effect 03 (Zone Camera) level. It implements it using camera sprites instead of zones.

Usage:

Create sprites in your level at points where you want cameras to be. Name them "cam1", "cam2", "cam3" and so on. (use Effects -> X-moto -> Other -> Change Id in Inkscape) I suggest using invisible sprites.

Then call UseZoneCamera(cam_count, keep_cams) to turn zone cameras on.

cam_count parameter is the number of cameras.

If keep_cams parameter is true, the sprites used as cameras will remain on the level. If it's false or not specified, the sprites will be deleted once zone camera is activated.

Example:

function OnLoad()
 UseZoneCamera(3)  -- if you have only 3 cameras: "cam1", "cam2", "cam3"
 return true
end 

You can also make some camera preferable. Change its Sprite scale value to do so. (in Effects -> X-moto -> Entities -> Sprite dialog) Set bigger value to make the camera more 'respected'. The script likes bigger cameras ;)

Test your level in 1280x1024 resolution to make sure player is always visible.

To use Zone Camera add Extended Camera script to your Lua file and then add this script:

-- UseZoneCamera
function UseZoneCamera(CamCount, keep_sprites)
 -- const
 local stick_radius = 0.5
 local y_coeff = 0.8
 -- /const
 
 local Cams = {}
 local CurCam
 local CamX, CamY = 0, 0
 
 local abs = math.abs
 local sqrt = math.sqrt
 
 local function ChangeCam()
   local x, y = Game.GetPlayerPosition()
   
   local function dist(cam)
     local c = Cams[cam]
     local dx, dy = abs(c[1] - x), abs(c[2] - y)*y_coeff
     return sqrt(dx*dx + dy*dy)/c[3]
   end
   
   local cam = CurCam
   local m = cam and (dist(cam) - stick_radius)
 
   for i = 1, CamCount do
     local d = dist(i)
     if not m or d < m then
       m = d
       cam = i
     end
   end
   CurCam = cam
   Game.CameraMoveAbs(Cams[cam][1], Cams[cam][2])
 end
 
 for i = 1, CamCount do
   local name = "cam"..i
   local cam = {Game.GetEntityPos(name)}
   cam[3] = Game.GetEntityRadius(name)
   Cams[i] = cam
   if not keep_sprites then  Game.KillEntity(name)  end
 end
 ChangeCam()
 
 local oldTick = Tick
 Tick = function()
   local ret = oldTick and oldTick() or true
   ChangeCam()
   return ret
 end
end
-- /UseZoneCamera

Author: GrayFace