diff options
author | the lemons <citrons@mondecitronne.com> | 2022-12-20 17:21:59 -0600 |
---|---|---|
committer | the lemons <citrons@mondecitronne.com> | 2022-12-21 00:12:16 -0600 |
commit | 15b1b3127e636a8a63ff9cdb11dbac68f5218ffe (patch) | |
tree | af710fe75c1081e2d9f119ae3280d2f662eea859 | |
parent | 94f7fc0d3e410a7c7dc6f6cffe0ed87238608391 (diff) |
objects have velocity
-rw-r--r-- | main.lua | 5 | ||||
-rw-r--r-- | obj.lua | 19 | ||||
-rw-r--r-- | objects/test.lua | 6 |
3 files changed, 21 insertions, 9 deletions
@@ -13,7 +13,10 @@ local cam = { } for i = 1, 100 do - obj.new("test", {math.random(1000), math.random(1000)}) + obj.new("test", {math.random() * 1000, math.random() * 1000}, { + avel = math.random() * 0.5 - 0.25, + vel = {math.random() * 0.5 - 0.25, math.random() * 0.5 - 0.25} + }) end local function view_scale() @@ -1,8 +1,10 @@ local world = require "world" +local pi = math.pi local obj = {} local types = {} + function obj.load_types() for _, f in ipairs(love.filesystem.getDirectoryItems "objects") do local ts = assert(love.filesystem.load("objects/"..f))() @@ -12,10 +14,11 @@ function obj.load_types() end end -function obj.new(type, pos, ...) +function obj.new(type, pos, data, ...) world.last_id = world.last_id + 1 local o = setmetatable( - {id = world.last_id, data = {pos = pos}, type = type}, obj) + {id = world.last_id, data = data or {}, type = type}, obj) + o.data.pos = pos o:init(...) return o end @@ -51,6 +54,14 @@ function obj:tick(...) chunk.objects[self.id] = self self.chunk = chunk end + if self.data.vel then + local vx, vy = unpack(self.data.vel) + self.data.pos[1] = self.data.pos[1] + vx + self.data.pos[2] = self.data.pos[2] + vy + end + if self.data.avel then + self.data.angle = (self.data.angle or 0) + self.data.avel / pi + end return self:overload("tick", ...) end @@ -70,10 +81,10 @@ function obj:init(...) return self:overload("init", ...) end -function obj:unload() +function obj:remove() self.chunk.objects[self.id] = nil world.objects[self.id] = nil - return self:overload "unload" + return self:overload "remove" end return obj diff --git a/objects/test.lua b/objects/test.lua index 5c2cb1b..d8c1a65 100644 --- a/objects/test.lua +++ b/objects/test.lua @@ -1,17 +1,15 @@ -local test = {} +local test = {radius = 4} function test:draw() line(-4, 4, 0, -4, 4, 4, -4, 4) - set_color(1, 0.6, 0.7) + set_color(4, 0.6, 0.7) line(4, -4, 0, 4, -4, -4, 4, -4) end function test:init() - self.data.angle = math.random() * math.pi end function test:tick() - self.data.angle = self.data.angle + 0.1 end return {test = test} |