diff --git a/src/data/init.lua b/src/data/init.lua index eac5d38..0ce676d 100644 --- a/src/data/init.lua +++ b/src/data/init.lua @@ -2,6 +2,8 @@ local cargo = require("lib.cargo") local aseLoader = require("lib.ase-loader") local pprint = require("lib.pprint") +-- TODO: Maybe add a texture atlas library for packing frame data +-- TODO: For production maybe use another type of loader? (https://github.com/elloramir/packer, https://github.com/EngineerSmith/Runtime-TextureAtlas) local function loadAsepriteSprite(filename) local sprite = {} local ase = aseLoader(filename) diff --git a/src/data/maps/playground.tmx b/src/data/maps/playground.tmx index 5402b5d..832fdc6 100644 --- a/src/data/maps/playground.tmx +++ b/src/data/maps/playground.tmx @@ -1,5 +1,5 @@ - + @@ -28,5 +28,12 @@ 1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1 - + + + + + + + + diff --git a/src/data/maps/test.lua b/src/data/maps/test.lua index 4180ae3..5226fda 100644 --- a/src/data/maps/test.lua +++ b/src/data/maps/test.lua @@ -10,7 +10,7 @@ return { tilewidth = 16, tileheight = 16, nextlayerid = 3, - nextobjectid = 1, + nextobjectid = 3, properties = {}, tilesets = { { @@ -74,7 +74,34 @@ return { parallaxx = 1, parallaxy = 1, properties = {}, - objects = {} + objects = { + { + id = 1, + name = "", + class = "", + shape = "point", + x = 67.3333, + y = 173.333, + width = 0, + height = 0, + rotation = 0, + visible = true, + properties = {} + }, + { + id = 2, + name = "", + class = "", + shape = "point", + x = 409.333, + y = 163.333, + width = 0, + height = 0, + rotation = 0, + visible = true, + properties = {} + } + } } } } diff --git a/src/lib/sti/init.lua b/src/lib/sti/init.lua index 9fecdab..50aec91 100644 --- a/src/lib/sti/init.lua +++ b/src/lib/sti/init.lua @@ -954,7 +954,7 @@ function Map:drawObjectLayer(layer) elseif object.shape == "polyline" then drawShape(object.polyline, "polyline") elseif object.shape == "point" then - lg.points(object.x, object.y) + -- lg.points(object.x, object.y) end end end diff --git a/src/states/main.lua b/src/states/main.lua index 3d86cc0..6253e13 100644 --- a/src/states/main.lua +++ b/src/states/main.lua @@ -110,11 +110,11 @@ function MainState:draw() ScreenScaler:finish() -- Draw UI on top - local w, h = self.downscaled_canvas:getDimensions() - ScreenScaler:start(1000, h/w * 1000) - love.graphics.setColor(1, 1, 1) - love.graphics.print("Hello World!") - ScreenScaler:finish() + -- local w, h = self.downscaled_canvas:getDimensions() + -- ScreenScaler:start(1000, h/w * 1000) + -- love.graphics.setColor(1, 1, 1) + -- love.graphics.print("Hello World!") + -- ScreenScaler:finish() -- Override scaling factors from UI, with scalers for the game ScreenScaler:overrideScaling(self.downscaled_canvas:getDimensions()) diff --git a/src/systems/bolt.lua b/src/systems/bolt.lua index e39294f..1a4b165 100644 --- a/src/systems/bolt.lua +++ b/src/systems/bolt.lua @@ -23,7 +23,6 @@ function Bolt:projectTrajectory(pos, step, count) local key_points = physics:project(pos, step.normalized, distance) local trajectory = {} - table.insert(trajectory, pos) local current_segment = 1 local current_distance = -step_size diff --git a/src/systems/debug.lua b/src/systems/debug.lua index cdae61c..c5fdd5a 100644 --- a/src/systems/debug.lua +++ b/src/systems/debug.lua @@ -9,6 +9,10 @@ local COLLIDER_COLOR = rgb(200, 20, 200) local DRAW_PING = false +function Debug:init() + self.current_player = 1 +end + function Debug:drawColliders() local physics = self.pool:getSystem(require("systems.physics")) love.graphics.setColor(COLLIDER_COLOR) @@ -35,6 +39,31 @@ function Debug:drawGrid() end end +function Debug:keypressed(key) + if key == "e" and love.keyboard.isDown("lshift") then + local PlayerSystem = self.pool:getSystem(require("systems.player")) + local player = PlayerSystem:spawnPlayer() + for _, e in ipairs(self.pool.groups.controllable_player.entities) do + e.controllable = false + self.pool:queue(e) + end + + player.controllable = true + self.pool:queue(player) + + self.current_player = #self.pool.groups.player.entities+1 + elseif key == "tab" then + local player_entities = self.pool.groups.player.entities + + player_entities[self.current_player].controllable = false + self.pool:queue(player_entities[self.current_player]) + + self.current_player = self.current_player % #player_entities + 1 + player_entities[self.current_player].controllable = true + self.pool:queue(player_entities[self.current_player]) + end +end + function Debug:draw() if DRAW_GRID then self:drawGrid() diff --git a/src/systems/map.lua b/src/systems/map.lua index 2b0889c..99409e1 100644 --- a/src/systems/map.lua +++ b/src/systems/map.lua @@ -1,5 +1,6 @@ local Map = {} local sti = require("lib.sti") +local Vector = require("lib.brinevector") function Map:init() self.map = sti("data/maps/test.lua", { "bump" }) @@ -10,6 +11,14 @@ function Map:update(dt) self.map:update(dt) end +function Map:listSpawnpoints() + local points = {} + for _, object in ipairs(self.map.layers.spawnpoints.objects) do + table.insert(points, Vector(object.x, object.y)) + end + return points +end + function Map:draw() love.graphics.setColor(1, 1, 1) self.map:draw() diff --git a/src/systems/physics.lua b/src/systems/physics.lua index 907b2d0..55fbfd7 100644 --- a/src/systems/physics.lua +++ b/src/systems/physics.lua @@ -40,9 +40,15 @@ function Physics:collisionFilter(entity, other) if entity.hidden then return end if other.hidden then return end - if entity.bolt then + local bolt = entity.bolt or other.bolt + local vel = entity.vel or other.vel + local player = entity.bolts or other.bolts + + if bolt and player then + return "cross" + elseif bolt then return "bounce" - elseif entity.vel or other.vel then + elseif (vel or vel) and not (entity.bolts and other.bolts) then return "slide" end end @@ -51,6 +57,7 @@ function Physics:resolveCollisions(e, dt) local targetPos = e.pos + e.vel * dt local ox = e.collider[1] local oy = e.collider[2] + self.bump:update(e, e.pos.x+ox, e.pos.y+oy) local x, y, cols = self.bump:move(e, targetPos.x+ox, targetPos.y+oy, self.boundCollisionFilter) local skip_moving = false @@ -61,7 +68,13 @@ function Physics:resolveCollisions(e, dt) end for _, col in ipairs(cols) do - if col.type == "bounce" then + if col.type == "cross" then + local player = col.other.bolts and col.other or e + local bolt = col.other.bolt and col.other or e + if player and bolt and bolt.owner ~= player then + self.pool:emit("hitPlayer", player, bolt) + end + elseif col.type == "bounce" then -- sx and sy and just number for flipped the direction when something -- bounces. -- When `normal.x` is zero, sx = 1, otherwise sx = -1. Same with sy diff --git a/src/systems/player.lua b/src/systems/player.lua index 9bb8583..3d1c7af 100644 --- a/src/systems/player.lua +++ b/src/systems/player.lua @@ -6,6 +6,8 @@ local controls = data.controls local AIMED_BOLT_DISTANCE = 15 +local BOLT_AMOUNT = 1 + local function getDirection(up_key, down_key, left_key, right_key) local dx = (love.keyboard.isDown(right_key) and 1 or 0) - (love.keyboard.isDown(left_key) and 1 or 0) @@ -147,16 +149,64 @@ function Player:shootBolt(player) bolt.pos = player.pos + player.aim_dir * AIMED_BOLT_DISTANCE bolt.vel = player.aim_dir * player.bolt_speed + bolt.owner = player self.pool:emit("boltShot", player, bolt) end function Player:storeBolt(player, bolt) + bolt.owner = nil table.insert(player.bolts, bolt) end +function Player:hitPlayer(player, bolt) + self:resetMap() +end + +local function createBolt() + return { + pos = Vec(), + vel = Vec(), + acc = Vec(), + sprite = { + name = "bolt", + variant = "idle", + }, + friction = 0.98, + max_speed = 400, + } +end + +function Player:resetMap() + local map = self.pool:getSystem(require("systems.map")) + local spawnpoints = map:listSpawnpoints() + + for _, bolt in ipairs(self.pool.groups.bolt.entities) do + self.pool:removeEntity(bolt) + end + + for i, player in ipairs(self.pool.groups.player.entities) do + for _, bolt in ipairs(player.bolts) do + self.pool:removeEntity(bolt) + end + + local spawnpoint = spawnpoints[(i - 1) % #spawnpoints + 1] + player.pos = spawnpoint + player.bolts = {} + for _=1, BOLT_AMOUNT do + local bolt = self.pool:queue(createBolt()) + self.pool:emit("storeBolt", player, bolt) + end + end +end + function Player:spawnPlayer() + local map = self.pool:getSystem(require("systems.map")) + local spawnpoints = map:listSpawnpoints() + local players = self.pool.groups.player.entities + local spawnpoint = spawnpoints[#players % #spawnpoints + 1] + local player = self.pool:queue{ - pos = Vec(100, 100), + pos = spawnpoint, vel = Vec(0, 0), acc = Vec(), @@ -172,18 +222,8 @@ function Player:spawnPlayer() collider = {-5, -5, 4, 8} } - for _=1, 10 do - local bolt = self.pool:queue{ - pos = Vec(100, 100), - vel = Vec(), - acc = Vec(), - sprite = { - name = "bolt", - variant = "idle", - }, - friction = 0.98, - max_speed = 400, - } + for _=1, BOLT_AMOUNT do + local bolt = self.pool:queue(createBolt()) self.pool:emit("storeBolt", player, bolt) end @@ -196,8 +236,11 @@ function Player:draw() local boltSystem = self.pool:getSystem(require("systems.bolt")) local pos = e.pos + e.aim_dir * AIMED_BOLT_DISTANCE*0 local vel = (e.aim_dir * e.bolt_speed).normalized * AIMED_BOLT_DISTANCE*1 - for _, position in ipairs(boltSystem:projectTrajectory(pos, vel, 5)) do - love.graphics.circle("line", position.x, position.y, 5) + + local point_amount = 5 + local points = boltSystem:projectTrajectory(pos, vel, point_amount+3) + for i=3, point_amount+3-1 do + love.graphics.circle("line", points[i].x, points[i].y, 5) end end end