2011年5月11日 星期三

在 LÖVE 實作追蹤系統

前言

之前用 actionscript3.0 做守塔遊戲的時候,有做了一個簡單的追蹤系統,現在把它移植到 LÖVE 上。

追蹤系統可以用在導彈,或是其它需要尾隨的應用。

 

範例程式下載

Tracker.love

 

情境說明

建立四個半徑不同大小的圓,追蹤的速度與半徑大小成反比;最小的圓追滑鼠,剩下的分別追比自己小一階的圓。

 

Screenshot

SNAGHTMLf1fcca

SNAGHTMLf2b4b0

 

程式碼(都在範例檔案內)

main.lua

 
require("lua/Circle")
require("lua/Tracker")
 
function love.load()
    mouse = {}
    
    circle1 = Circle.create(5)
    circle2 = Circle.create(10)
    circle3 = Circle.create(15)
    circle4 = Circle.create(20)
end
 
function love.update(dt)
    -- we need tracker/target provide x,y index for Tracker system
    
    -- let mouse provide x,y index
    mouse.x, mouse.y = love.mouse.getPosition()
    
    -- args = speed, tracker, target
    Tracker.trace(8, circle1, mouse)
    Tracker.trace(6, circle2, circle1)
    Tracker.trace(4, circle3, circle2)
    Tracker.trace(2, circle4, circle3)
end
 
function love.draw()
    circle1:draw()
    circle2:draw()
    circle3:draw()
    circle4:draw()
end

conf.lua

function love.conf(t)
    t.title = "Tracker"         -- The title of the window the game is in (string)
    t.author = "bmzz"           -- The author of the game (string)
    t.identity = nil            -- The name of the save directory (string)
    t.version = 072             -- The LOVE version this game was made for (number)
    t.console = false           -- Attach a console (boolean, Windows only)
    t.screen.width = 800        -- The window width (number)
    t.screen.height = 600       -- The window height (number)
    t.screen.fullscreen = false -- Enable fullscreen (boolean)
    t.screen.vsync = true       -- Enable vertical sync (boolean)
    t.screen.fsaa = 0           -- The number of FSAA-buffers (number)
    t.modules.joystick = false  -- Enable the joystick module (boolean)
    t.modules.audio = false     -- Enable the audio module (boolean)
    t.modules.keyboard = false  -- Enable the keyboard module (boolean)
    t.modules.event = true      -- Enable the event module (boolean)
    t.modules.image = false     -- Enable the image module (boolean)
    t.modules.graphics = true   -- Enable the graphics module (boolean)
    t.modules.timer = true      -- Enable the timer module (boolean)
    t.modules.mouse = true      -- Enable the mouse module (boolean)
    t.modules.sound = false     -- Enable the sound module (boolean)
    t.modules.physics = false   -- Enable the physics module (boolean)
end

lua/Circle.lua

 
Circle = {}
Circle.__index = Circle
 
function Circle.create(radius, attributes)
    attrs = attributes or {}
    
    local self = {}
    setmetatable(self, Circle)
    
    self.radius = radius
    self.segments = radius / 2
    if self.segments < 50 then self.segments = 50 end
    
    self.mode = attrs.mode or "line"
    
    self.x = attrs.x or 0
    self.y = attrs.y or 0
    
    self.color = attrs.color or {255, 255, 255}
    
    return self
end
 
function Circle:draw()
    love.graphics.setColor(self.color)
    love.graphics.circle(self.mode, self.x, self.y, self.radius, self.segments)
end

lua/Tracker.lua

 
Tracker = {}
 
function Tracker.trace(speed, tracker, target)
    local diffx = target.x - tracker.x
    local diffy = target.y - tracker.y
    
    local dist = math.sqrt(diffx ^ 2 + diffy ^ 2)
    local perc = speed / dist
    
    if dist > speed then
        tracker.x = tracker.x + diffx * perc
        tracker.y = tracker.y + diffy * perc
    else
        tracker.x = target.x
        tracker.y = target.y
    end
end

沒有留言:

張貼留言