add beholder and get drawing working
"
This commit is contained in:
parent
0a1d655267
commit
c4c33e4726
6 changed files with 246 additions and 26 deletions
|
|
@ -101,4 +101,4 @@
|
||||||
(fs/write-lines "../levels.fnl" ["(local levels"
|
(fs/write-lines "../levels.fnl" ["(local levels"
|
||||||
(str/replace (pr-str levels) #",+" "")
|
(str/replace (pr-str levels) #",+" "")
|
||||||
")\n\n"
|
")\n\n"
|
||||||
"{ :levels levels }"])
|
"levels"])
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
175
two_player_cleaning_game/libs/beholder.lua
Normal file
175
two_player_cleaning_game/libs/beholder.lua
Normal file
|
|
@ -0,0 +1,175 @@
|
||||||
|
-- beholder.lua - v2.1.1 (2011-11)
|
||||||
|
|
||||||
|
-- Copyright (c) 2011 Enrique García Cota
|
||||||
|
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
|
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN callback OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
local function copy(t)
|
||||||
|
local c = {}
|
||||||
|
for i = 1, #t do c[i] = t[i] end
|
||||||
|
return c
|
||||||
|
end
|
||||||
|
|
||||||
|
local function hash2array(t)
|
||||||
|
local arr, i = {}, 0
|
||||||
|
for _, v in pairs(t) do
|
||||||
|
i = i + 1
|
||||||
|
arr[i] = v
|
||||||
|
end
|
||||||
|
return arr, i
|
||||||
|
end
|
||||||
|
-- private Node class
|
||||||
|
|
||||||
|
local nodesById = nil
|
||||||
|
local root = nil
|
||||||
|
|
||||||
|
local function newNode()
|
||||||
|
return { callbacks = {}, children = setmetatable({}, { __mode = "k" }) }
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local function findNodeById(id)
|
||||||
|
return nodesById[id]
|
||||||
|
end
|
||||||
|
|
||||||
|
local function findOrCreateChildNode(self, key)
|
||||||
|
self.children[key] = self.children[key] or newNode()
|
||||||
|
return self.children[key]
|
||||||
|
end
|
||||||
|
|
||||||
|
local function findOrCreateDescendantNode(self, keys)
|
||||||
|
local node = self
|
||||||
|
for i = 1, #keys do
|
||||||
|
node = findOrCreateChildNode(node, keys[i])
|
||||||
|
end
|
||||||
|
return node
|
||||||
|
end
|
||||||
|
|
||||||
|
local function invokeNodeCallbacks(self, params)
|
||||||
|
-- copy the hash into an array, for safety (self-erasures)
|
||||||
|
local callbacks, count = hash2array(self.callbacks)
|
||||||
|
for i = 1, #callbacks do
|
||||||
|
callbacks[i](unpack(params))
|
||||||
|
end
|
||||||
|
return count
|
||||||
|
end
|
||||||
|
|
||||||
|
local function invokeAllNodeCallbacksInSubTree(self, params)
|
||||||
|
local counter = invokeNodeCallbacks(self, params)
|
||||||
|
for _, child in pairs(self.children) do
|
||||||
|
counter = counter + invokeAllNodeCallbacksInSubTree(child, params)
|
||||||
|
end
|
||||||
|
return counter
|
||||||
|
end
|
||||||
|
|
||||||
|
local function invokeNodeCallbacksFromPath(self, path)
|
||||||
|
local node = self
|
||||||
|
local params = copy(path)
|
||||||
|
local counter = invokeNodeCallbacks(node, params)
|
||||||
|
|
||||||
|
for i = 1, #path do
|
||||||
|
node = node.children[path[i]]
|
||||||
|
if not node then break end
|
||||||
|
table.remove(params, 1)
|
||||||
|
counter = counter + invokeNodeCallbacks(node, params)
|
||||||
|
end
|
||||||
|
|
||||||
|
return counter
|
||||||
|
end
|
||||||
|
|
||||||
|
local function addCallbackToNode(self, callback)
|
||||||
|
local id = {}
|
||||||
|
self.callbacks[id] = callback
|
||||||
|
nodesById[id] = self
|
||||||
|
return id
|
||||||
|
end
|
||||||
|
|
||||||
|
local function removeCallbackFromNode(self, id)
|
||||||
|
self.callbacks[id] = nil
|
||||||
|
nodesById[id] = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
------ beholder table
|
||||||
|
|
||||||
|
local beholder = {}
|
||||||
|
|
||||||
|
|
||||||
|
-- beholder private functions/vars
|
||||||
|
|
||||||
|
local groups = nil
|
||||||
|
local currentGroupId = nil
|
||||||
|
|
||||||
|
local function addIdToCurrentGroup(id)
|
||||||
|
if currentGroupId then
|
||||||
|
groups[currentGroupId] = groups[currentGroupId] or setmetatable({}, { __mode = "k" })
|
||||||
|
local group = groups[currentGroupId]
|
||||||
|
group[#group + 1] = id
|
||||||
|
end
|
||||||
|
return id
|
||||||
|
end
|
||||||
|
|
||||||
|
local function stopObservingGroup(group)
|
||||||
|
local count = #group
|
||||||
|
for i = 1, count do
|
||||||
|
beholder.stopObserving(group[i])
|
||||||
|
end
|
||||||
|
return count
|
||||||
|
end
|
||||||
|
|
||||||
|
local function falseIfZero(n)
|
||||||
|
return n > 0 and n
|
||||||
|
end
|
||||||
|
|
||||||
|
local function extractEventAndCallbackFromParams(params)
|
||||||
|
assert(#params > 0,
|
||||||
|
"beholder.observe requires at least one parameter - the callback. You usually want to use two, i.e.: beholder.observe('EVENT', callback)")
|
||||||
|
local callback = table.remove(params, #params)
|
||||||
|
return params, callback
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
------ Public interface
|
||||||
|
|
||||||
|
function beholder.observe(...)
|
||||||
|
local event, callback = extractEventAndCallbackFromParams({ ... })
|
||||||
|
local node = findOrCreateDescendantNode(root, event)
|
||||||
|
return addIdToCurrentGroup(addCallbackToNode(node, callback))
|
||||||
|
end
|
||||||
|
|
||||||
|
function beholder.stopObserving(id)
|
||||||
|
local node = findNodeById(id)
|
||||||
|
if node then removeCallbackFromNode(node, id) end
|
||||||
|
|
||||||
|
local group, count = groups[id], 0
|
||||||
|
if group then count = stopObservingGroup(group) end
|
||||||
|
|
||||||
|
return (node or count > 0) and true or false
|
||||||
|
end
|
||||||
|
|
||||||
|
function beholder.group(groupId, f)
|
||||||
|
assert(not currentGroupId, "beholder.group can not be nested!")
|
||||||
|
currentGroupId = groupId
|
||||||
|
f()
|
||||||
|
currentGroupId = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
function beholder.trigger(...)
|
||||||
|
return falseIfZero(invokeNodeCallbacksFromPath(root, { ... }))
|
||||||
|
end
|
||||||
|
|
||||||
|
function beholder.triggerAll(...)
|
||||||
|
return falseIfZero(invokeAllNodeCallbacksInSubTree(root, { ... }))
|
||||||
|
end
|
||||||
|
|
||||||
|
function beholder.reset()
|
||||||
|
root = newNode()
|
||||||
|
nodesById = setmetatable({}, { __mode = "k" })
|
||||||
|
groups = {}
|
||||||
|
currentGroupId = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
beholder.reset()
|
||||||
|
|
||||||
|
return beholder
|
||||||
|
|
@ -175,6 +175,7 @@
|
||||||
(love.window.setMode screen.screen-w screen.screen-h)
|
(love.window.setMode screen.screen-w screen.screen-h)
|
||||||
; (tset screen :canvas (love.graphics.newCanvas screen.canvas-w screen.canvas-h))
|
; (tset screen :canvas (love.graphics.newCanvas screen.canvas-w screen.canvas-h))
|
||||||
(set pool (tutorial.load screen))
|
(set pool (tutorial.load screen))
|
||||||
|
(pool:emit :load :tutorial)
|
||||||
; (utils.debug-print world)
|
; (utils.debug-print world)
|
||||||
; (load-assets)
|
; (load-assets)
|
||||||
; (load-level :tutorial)
|
; (load-level :tutorial)
|
||||||
|
|
@ -292,25 +293,6 @@ while 1 do love.event.push('stdin', io.read('*line')) end") :start))
|
||||||
; (local directions [:e :se :s :sw :w :nw :n :ne])
|
; (local directions [:e :se :s :sw :w :nw :n :ne])
|
||||||
; (. directions (+ (% section 8) 1)))
|
; (. directions (+ (% section 8) 1)))
|
||||||
|
|
||||||
; (fn draw-player []
|
|
||||||
; "draw player sprite and hitbox"
|
|
||||||
; (reset-color)
|
|
||||||
; (love.graphics.draw
|
|
||||||
; player-art.player-sprite
|
|
||||||
; (. player-art.player1.quads (angle-to-direction player.rot))
|
|
||||||
; player.x player.y)
|
|
||||||
; ;; draw player hitbox and direction line
|
|
||||||
; (when debug
|
|
||||||
; (set-color :black)
|
|
||||||
; (love.graphics.rectangle "line" player.x player.y player.w player.h)
|
|
||||||
; (love.graphics.push)
|
|
||||||
; (let [ox (+ player.x (/ 25 2)) oy (+ player.y (/ 25 2))]
|
|
||||||
; (love.graphics.translate ox oy)
|
|
||||||
; (love.graphics.rotate player.rot)
|
|
||||||
; (love.graphics.line 0 0 35 0))
|
|
||||||
; (love.graphics.pop)
|
|
||||||
; ))
|
|
||||||
|
|
||||||
; (fn draw-objects []
|
; (fn draw-objects []
|
||||||
; (each [_ obj (pairs objects.list)]
|
; (each [_ obj (pairs objects.list)]
|
||||||
; ; (print (fennel.view obj))
|
; ; (print (fennel.view obj))
|
||||||
|
|
|
||||||
58
two_player_cleaning_game/src/entities/player.fnl
Normal file
58
two_player_cleaning_game/src/entities/player.fnl
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
(local colors (require "src/colors.fnl"))
|
||||||
|
(local levels (require "levels.fnl"))
|
||||||
|
(local assets (require "src/assets.fnl"))
|
||||||
|
|
||||||
|
(lambda angle-to-direction [angle]
|
||||||
|
"Convert angle (radians) to compass direction keyword"
|
||||||
|
(local tau (* 2 math.pi))
|
||||||
|
; Normalize angle to 0-2π range
|
||||||
|
(local normalized (% (+ angle tau) tau))
|
||||||
|
; Offset by 22.5° (π/8) so section boundaries align with directions
|
||||||
|
(local offset (+ normalized (/ math.pi 8)))
|
||||||
|
; Find which 45° section (π/4) it falls into
|
||||||
|
(local section (math.floor (/ offset (/ math.pi 4))))
|
||||||
|
; Map to compass directions
|
||||||
|
(local directions [:e :se :s :sw :w :nw :n :ne])
|
||||||
|
(. directions (+ (% section 8) 1)))
|
||||||
|
|
||||||
|
(local player { :x 50 :y 50 :w 25 :h 25 :speed 80 :battery 100 :rot 0 })
|
||||||
|
(fn player.load [self level-key]
|
||||||
|
(set self.battery 100)
|
||||||
|
(set self.x (. levels :levels level-key :spawns :player-1 :x))
|
||||||
|
(set self.y (. levels :levels level-key :spawns :player-1 :y))
|
||||||
|
; (bump-world:add player player.x player.y player.w player.h))
|
||||||
|
(set self.quads
|
||||||
|
(let [
|
||||||
|
(w h) (assets.player-sprite:getDimensions)]
|
||||||
|
{
|
||||||
|
:n (love.graphics.newQuad 0 0 25 25 w h)
|
||||||
|
:s (love.graphics.newQuad 25 0 25 25 w h)
|
||||||
|
:ne (love.graphics.newQuad 50 0 25 25 w h)
|
||||||
|
:e (love.graphics.newQuad 75 0 25 25 w h)
|
||||||
|
:se (love.graphics.newQuad 100 0 25 25 w h)
|
||||||
|
:sw (love.graphics.newQuad 125 0 25 25 w h)
|
||||||
|
:w (love.graphics.newQuad 150 0 25 25 w h)
|
||||||
|
:nw (love.graphics.newQuad 175 0 25 25 w h)
|
||||||
|
}))
|
||||||
|
)
|
||||||
|
|
||||||
|
(fn player.draw [self state]
|
||||||
|
"draw player sprite and hitbox"
|
||||||
|
(colors:reset-color)
|
||||||
|
(love.graphics.draw
|
||||||
|
assets.player-sprite
|
||||||
|
(. self.quads (angle-to-direction player.rot))
|
||||||
|
self.x self.y)
|
||||||
|
;; draw player hitbox and direction line
|
||||||
|
(when state.debug
|
||||||
|
(colors:set-color :black)
|
||||||
|
(love.graphics.rectangle "line" self.x self.y self.w self.h)
|
||||||
|
(love.graphics.push)
|
||||||
|
(let [ox (+ self.x (/ 25 2)) oy (+ self.y (/ 25 2))]
|
||||||
|
(love.graphics.translate ox oy)
|
||||||
|
(love.graphics.rotate self.rot)
|
||||||
|
(love.graphics.line 0 0 35 0))
|
||||||
|
(love.graphics.pop)))
|
||||||
|
|
||||||
|
|
||||||
|
player
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
(local color (require "src/colors.fnl"))
|
(local color (require "src/colors.fnl"))
|
||||||
(local nata (require "libs/nata"))
|
(local nata (require "libs/nata"))
|
||||||
(local utils (require "src/utils.fnl"))
|
(local utils (require "src/utils.fnl"))
|
||||||
|
(local beholder (require "libs/beholder"))
|
||||||
|
(local player (require "src/entities/player.fnl"))
|
||||||
|
|
||||||
|
|
||||||
(lambda gen-renderer [screen canvas]
|
(lambda gen-renderer [screen canvas]
|
||||||
(let [renderer {}]
|
(let [renderer {}]
|
||||||
|
|
@ -23,15 +26,18 @@
|
||||||
; (local debug true)
|
; (local debug true)
|
||||||
|
|
||||||
(fn load [screen]
|
(fn load [screen]
|
||||||
(let [canvas (love.graphics.newCanvas screen.canvas-w screen.canvas-h)]
|
(let [
|
||||||
(nata.new {
|
canvas (love.graphics.newCanvas screen.canvas-w screen.canvas-h)
|
||||||
|
pool (nata.new {
|
||||||
; :groups {
|
; :groups {
|
||||||
; gravity = {filter = {'gravity'}},
|
; gravity = {filter = {'gravity'}},
|
||||||
; },
|
; },
|
||||||
:systems [
|
:systems [
|
||||||
(gen-renderer screen canvas)
|
(gen-renderer screen canvas)
|
||||||
]
|
]})]
|
||||||
})))
|
(pool:queue player)
|
||||||
|
pool
|
||||||
|
))
|
||||||
|
|
||||||
{
|
{
|
||||||
: load
|
: load
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue