1
0

reset map when any player gets hit

This commit is contained in:
Rokas Puzonas 2022-08-13 01:32:29 +00:00
parent f19fbda061
commit 0351d38b10
10 changed files with 158 additions and 29 deletions

View File

@ -2,6 +2,8 @@ local cargo = require("lib.cargo")
local aseLoader = require("lib.ase-loader") local aseLoader = require("lib.ase-loader")
local pprint = require("lib.pprint") 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 function loadAsepriteSprite(filename)
local sprite = {} local sprite = {}
local ase = aseLoader(filename) local ase = aseLoader(filename)

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<map version="1.9" tiledversion="1.9.0" orientation="orthogonal" renderorder="right-down" width="30" height="20" tilewidth="16" tileheight="16" infinite="0" nextlayerid="3" nextobjectid="1"> <map version="1.9" tiledversion="1.9.0" orientation="orthogonal" renderorder="right-down" width="30" height="20" tilewidth="16" tileheight="16" infinite="0" nextlayerid="3" nextobjectid="3">
<editorsettings> <editorsettings>
<export target="test.lua" format="lua"/> <export target="test.lua" format="lua"/>
</editorsettings> </editorsettings>
@ -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 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
</data> </data>
</layer> </layer>
<objectgroup id="2" name="spawnpoints"/> <objectgroup id="2" name="spawnpoints">
<object id="1" x="67.3333" y="173.333">
<point/>
</object>
<object id="2" x="409.333" y="163.333">
<point/>
</object>
</objectgroup>
</map> </map>

View File

@ -10,7 +10,7 @@ return {
tilewidth = 16, tilewidth = 16,
tileheight = 16, tileheight = 16,
nextlayerid = 3, nextlayerid = 3,
nextobjectid = 1, nextobjectid = 3,
properties = {}, properties = {},
tilesets = { tilesets = {
{ {
@ -74,7 +74,34 @@ return {
parallaxx = 1, parallaxx = 1,
parallaxy = 1, parallaxy = 1,
properties = {}, 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 = {}
}
}
} }
} }
} }

View File

@ -954,7 +954,7 @@ function Map:drawObjectLayer(layer)
elseif object.shape == "polyline" then elseif object.shape == "polyline" then
drawShape(object.polyline, "polyline") drawShape(object.polyline, "polyline")
elseif object.shape == "point" then elseif object.shape == "point" then
lg.points(object.x, object.y) -- lg.points(object.x, object.y)
end end
end end
end end

View File

@ -110,11 +110,11 @@ function MainState:draw()
ScreenScaler:finish() ScreenScaler:finish()
-- Draw UI on top -- Draw UI on top
local w, h = self.downscaled_canvas:getDimensions() -- local w, h = self.downscaled_canvas:getDimensions()
ScreenScaler:start(1000, h/w * 1000) -- ScreenScaler:start(1000, h/w * 1000)
love.graphics.setColor(1, 1, 1) -- love.graphics.setColor(1, 1, 1)
love.graphics.print("Hello World!") -- love.graphics.print("Hello World!")
ScreenScaler:finish() -- ScreenScaler:finish()
-- Override scaling factors from UI, with scalers for the game -- Override scaling factors from UI, with scalers for the game
ScreenScaler:overrideScaling(self.downscaled_canvas:getDimensions()) ScreenScaler:overrideScaling(self.downscaled_canvas:getDimensions())

View File

@ -23,7 +23,6 @@ function Bolt:projectTrajectory(pos, step, count)
local key_points = physics:project(pos, step.normalized, distance) local key_points = physics:project(pos, step.normalized, distance)
local trajectory = {} local trajectory = {}
table.insert(trajectory, pos)
local current_segment = 1 local current_segment = 1
local current_distance = -step_size local current_distance = -step_size

View File

@ -9,6 +9,10 @@ local COLLIDER_COLOR = rgb(200, 20, 200)
local DRAW_PING = false local DRAW_PING = false
function Debug:init()
self.current_player = 1
end
function Debug:drawColliders() function Debug:drawColliders()
local physics = self.pool:getSystem(require("systems.physics")) local physics = self.pool:getSystem(require("systems.physics"))
love.graphics.setColor(COLLIDER_COLOR) love.graphics.setColor(COLLIDER_COLOR)
@ -35,6 +39,31 @@ function Debug:drawGrid()
end end
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() function Debug:draw()
if DRAW_GRID then if DRAW_GRID then
self:drawGrid() self:drawGrid()

View File

@ -1,5 +1,6 @@
local Map = {} local Map = {}
local sti = require("lib.sti") local sti = require("lib.sti")
local Vector = require("lib.brinevector")
function Map:init() function Map:init()
self.map = sti("data/maps/test.lua", { "bump" }) self.map = sti("data/maps/test.lua", { "bump" })
@ -10,6 +11,14 @@ function Map:update(dt)
self.map:update(dt) self.map:update(dt)
end 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() function Map:draw()
love.graphics.setColor(1, 1, 1) love.graphics.setColor(1, 1, 1)
self.map:draw() self.map:draw()

View File

@ -40,9 +40,15 @@ function Physics:collisionFilter(entity, other)
if entity.hidden then return end if entity.hidden then return end
if other.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" return "bounce"
elseif entity.vel or other.vel then elseif (vel or vel) and not (entity.bolts and other.bolts) then
return "slide" return "slide"
end end
end end
@ -51,6 +57,7 @@ function Physics:resolveCollisions(e, dt)
local targetPos = e.pos + e.vel * dt local targetPos = e.pos + e.vel * dt
local ox = e.collider[1] local ox = e.collider[1]
local oy = e.collider[2] 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 x, y, cols = self.bump:move(e, targetPos.x+ox, targetPos.y+oy, self.boundCollisionFilter)
local skip_moving = false local skip_moving = false
@ -61,7 +68,13 @@ function Physics:resolveCollisions(e, dt)
end end
for _, col in ipairs(cols) do 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 -- sx and sy and just number for flipped the direction when something
-- bounces. -- bounces.
-- When `normal.x` is zero, sx = 1, otherwise sx = -1. Same with sy -- When `normal.x` is zero, sx = 1, otherwise sx = -1. Same with sy

View File

@ -6,6 +6,8 @@ local controls = data.controls
local AIMED_BOLT_DISTANCE = 15 local AIMED_BOLT_DISTANCE = 15
local BOLT_AMOUNT = 1
local function getDirection(up_key, down_key, left_key, right_key) local function getDirection(up_key, down_key, left_key, right_key)
local dx = (love.keyboard.isDown(right_key) and 1 or 0) local dx = (love.keyboard.isDown(right_key) and 1 or 0)
- (love.keyboard.isDown(left_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.pos = player.pos + player.aim_dir * AIMED_BOLT_DISTANCE
bolt.vel = player.aim_dir * player.bolt_speed bolt.vel = player.aim_dir * player.bolt_speed
bolt.owner = player
self.pool:emit("boltShot", player, bolt) self.pool:emit("boltShot", player, bolt)
end end
function Player:storeBolt(player, bolt) function Player:storeBolt(player, bolt)
bolt.owner = nil
table.insert(player.bolts, bolt) table.insert(player.bolts, bolt)
end 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() 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{ local player = self.pool:queue{
pos = Vec(100, 100), pos = spawnpoint,
vel = Vec(0, 0), vel = Vec(0, 0),
acc = Vec(), acc = Vec(),
@ -172,18 +222,8 @@ function Player:spawnPlayer()
collider = {-5, -5, 4, 8} collider = {-5, -5, 4, 8}
} }
for _=1, 10 do for _=1, BOLT_AMOUNT do
local bolt = self.pool:queue{ local bolt = self.pool:queue(createBolt())
pos = Vec(100, 100),
vel = Vec(),
acc = Vec(),
sprite = {
name = "bolt",
variant = "idle",
},
friction = 0.98,
max_speed = 400,
}
self.pool:emit("storeBolt", player, bolt) self.pool:emit("storeBolt", player, bolt)
end end
@ -196,8 +236,11 @@ function Player:draw()
local boltSystem = self.pool:getSystem(require("systems.bolt")) local boltSystem = self.pool:getSystem(require("systems.bolt"))
local pos = e.pos + e.aim_dir * AIMED_BOLT_DISTANCE*0 local pos = e.pos + e.aim_dir * AIMED_BOLT_DISTANCE*0
local vel = (e.aim_dir * e.bolt_speed).normalized * AIMED_BOLT_DISTANCE*1 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 end
end end