Compare commits
4 Commits
ea4504bdb4
...
d4c79731b0
Author | SHA1 | Date | |
---|---|---|---|
d4c79731b0 | |||
40c9af4803 | |||
4900c50850 | |||
651ea584af |
5
CREDITS.txt
Normal file
5
CREDITS.txt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
PATH: NAME - HYPERLINK - LICENSE
|
||||||
|
|
||||||
|
data/images/duckie.png: ChickenTeddy - https://opengameart.org/content/duckie - CC-BY 4.0
|
||||||
|
data/images/measure001a.png: KenneyNL - https://www.kenney.nl/assets/prototype-textures - CC0
|
||||||
|
data/images/tongue.png: KenneyNL - ? - CC0
|
BIN
data/fonts/Lunchtype21_Regular.ttf
Normal file
BIN
data/fonts/Lunchtype21_Regular.ttf
Normal file
Binary file not shown.
BIN
data/images/duckie.png
Normal file
BIN
data/images/duckie.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.3 KiB |
104
data/scripts/classes/duck.lua
Normal file
104
data/scripts/classes/duck.lua
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
local Vector3 = require "types.vector3"
|
||||||
|
local Signal = require "types.signal"
|
||||||
|
|
||||||
|
local TEXTURE = "images/duckie.png"
|
||||||
|
|
||||||
|
local States = {
|
||||||
|
IDLE = 1,
|
||||||
|
WANDER = 2,
|
||||||
|
CHASE = 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
local WANDER_TIME = 1.0
|
||||||
|
local IDLE_TIME = 1.0
|
||||||
|
|
||||||
|
local Duck = {
|
||||||
|
position = {},
|
||||||
|
velocity = {},
|
||||||
|
direction = {},
|
||||||
|
|
||||||
|
target = nil,
|
||||||
|
state = States.IDLE,
|
||||||
|
|
||||||
|
wander_timer = 0,
|
||||||
|
|
||||||
|
speed = 0.02,
|
||||||
|
chase_speed = 0.04,
|
||||||
|
accel = 0.5,
|
||||||
|
|
||||||
|
STATES = States,
|
||||||
|
|
||||||
|
AteFeed = Signal.new()
|
||||||
|
}
|
||||||
|
|
||||||
|
Duck.__index = Duck
|
||||||
|
|
||||||
|
function Duck.new(position)
|
||||||
|
local d = util.shallow_copy(Duck)
|
||||||
|
d.position = position
|
||||||
|
d.velocity = Vector3()
|
||||||
|
d.direction = Vector3(0, 0, -1):rotated(Vector3.UP, util.random_float(-math.pi, math.pi))
|
||||||
|
|
||||||
|
return setmetatable(d, Duck)
|
||||||
|
end
|
||||||
|
|
||||||
|
function Duck:tick(ctx)
|
||||||
|
if self.state == States.IDLE then
|
||||||
|
self:idle(ctx.frame_duration)
|
||||||
|
elseif self.state == States.WANDER then
|
||||||
|
self:wander(ctx.frame_duration)
|
||||||
|
elseif self.state == States.CHASE then
|
||||||
|
self:chase(ctx.frame_duration)
|
||||||
|
end
|
||||||
|
draw_billboard{position = self.position, size = {x = 0.4, y = 0.4}, texture = TEXTURE}
|
||||||
|
end
|
||||||
|
|
||||||
|
function Duck:idle(delta)
|
||||||
|
self.wander_timer = self.wander_timer + delta
|
||||||
|
if self.wander_timer >= IDLE_TIME then
|
||||||
|
self.state = States.WANDER
|
||||||
|
self.wander_timer = 0
|
||||||
|
self.direction = Vector3(0, 0, -1):rotated(Vector3.UP, math.random() * (math.pi * 2.0))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Duck:wander(delta)
|
||||||
|
self.wander_timer = self.wander_timer + delta
|
||||||
|
if self.wander_timer >= WANDER_TIME then
|
||||||
|
self.state = States.IDLE
|
||||||
|
self.wander_timer = 0
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local target_vel = self.direction * self.speed
|
||||||
|
self.velocity = self.velocity:lerp(target_vel, self.accel)
|
||||||
|
self.position = self.position + self.velocity
|
||||||
|
end
|
||||||
|
|
||||||
|
function Duck:chase(delta)
|
||||||
|
local hpos = self.position:horizontal()
|
||||||
|
local fhpos = self.target.position:horizontal()
|
||||||
|
|
||||||
|
local dist = hpos:distance_to(fhpos)
|
||||||
|
if dist <= 0.1 then
|
||||||
|
self.AteFeed:emit(self.target)
|
||||||
|
self.wander_timer = 0
|
||||||
|
self.state = States.IDLE
|
||||||
|
self.target = nil
|
||||||
|
else
|
||||||
|
local dir = self.position:horizontal():direction_to(self.target.position:horizontal())
|
||||||
|
local target_vel = dir * self.chase_speed
|
||||||
|
self.velocity = self.velocity:lerp(target_vel, self.accel)
|
||||||
|
self.position = self.position + self.velocity
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Duck:start_chase(feed)
|
||||||
|
if self.state == States.CHASE then return end
|
||||||
|
self.state = States.CHASE
|
||||||
|
self.target = feed
|
||||||
|
feed.occupied = true
|
||||||
|
end
|
||||||
|
|
||||||
|
return Duck
|
@ -7,6 +7,7 @@ local Feed = {
|
|||||||
direction = {},
|
direction = {},
|
||||||
velocity = {},
|
velocity = {},
|
||||||
landed = false,
|
landed = false,
|
||||||
|
occupied = false,
|
||||||
|
|
||||||
speed = 0.15,
|
speed = 0.15,
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,6 @@ function Player:tick(ctx)
|
|||||||
local direction = ((camera_forward * forward_input) + (camera_right * strafe_input)):normalized()
|
local direction = ((camera_forward * forward_input) + (camera_right * strafe_input)):normalized()
|
||||||
local target_vel = direction * self.speed
|
local target_vel = direction * self.speed
|
||||||
self.velocity = self.velocity:lerp(target_vel, self.accel)
|
self.velocity = self.velocity:lerp(target_vel, self.accel)
|
||||||
-- self.velocity = target_vel
|
|
||||||
|
|
||||||
if input_action_just_pressed{name = "throw"} then
|
if input_action_just_pressed{name = "throw"} then
|
||||||
self.ThrowPressed:emit(self.position:copy(), camera_forward:copy())
|
self.ThrowPressed:emit(self.position:copy(), camera_forward:copy())
|
||||||
|
@ -1,12 +1,29 @@
|
|||||||
util = require "util"
|
util = require "util"
|
||||||
local player = require "classes.player"
|
local player = require "classes.player"
|
||||||
local Vector3 = require "types.vector3"
|
local Vector3 = require "types.vector3"
|
||||||
|
local List = require "types.list"
|
||||||
|
|
||||||
local Feed = require "classes.feed"
|
local Feed = require "classes.feed"
|
||||||
local feed = {}
|
local feed = List()
|
||||||
|
|
||||||
|
local Duck = require "classes.duck"
|
||||||
|
local ducks = List{Duck.new(Vector3(0, 0.4, 3))}
|
||||||
|
|
||||||
local function create_feed(position, direction)
|
local function create_feed(position, direction)
|
||||||
table.insert(feed, Feed.new(position, direction))
|
local f = Feed.new(position, direction)
|
||||||
|
feed:push(f)
|
||||||
|
local eligible_ducks = ducks:filter(
|
||||||
|
function (duck)
|
||||||
|
return duck.state ~= Duck.STATES.CHASE
|
||||||
|
end
|
||||||
|
)
|
||||||
|
if #eligible_ducks == 0 then return end
|
||||||
|
eligible_ducks[1]:start_chase(f)
|
||||||
|
f.occupied = true
|
||||||
|
end
|
||||||
|
|
||||||
|
local function delete_feed(f)
|
||||||
|
util.list_remove_value(feed, f)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- called every frame, with constant delta time
|
-- called every frame, with constant delta time
|
||||||
@ -18,22 +35,27 @@ function game_tick()
|
|||||||
capture = false,
|
capture = false,
|
||||||
}
|
}
|
||||||
player.ThrowPressed:connect(create_feed)
|
player.ThrowPressed:connect(create_feed)
|
||||||
|
for _, v in ipairs(ducks) do
|
||||||
|
v.AteFeed:connect(delete_feed)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
ctx.mouse_capture = ctx.udata.capture
|
ctx.mouse_capture = ctx.udata.capture
|
||||||
input_action{name = "toggle_mouse", control = "ESCAPE"}
|
input_action{name = "toggle_mouse", control = "ESCAPE"}
|
||||||
|
|
||||||
if input_action_just_pressed{name = "toggle_mouse"} then
|
if input_action_just_pressed{name = "toggle_mouse"} then
|
||||||
ctx.udata.capture = not ctx.udata.capture
|
ctx.udata.capture = not ctx.udata.capture
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, v in pairs(feed) do
|
for _, v in ipairs(feed) do
|
||||||
|
v:tick(ctx)
|
||||||
|
end
|
||||||
|
|
||||||
|
for _, v in ipairs(ducks) do
|
||||||
v:tick(ctx)
|
v:tick(ctx)
|
||||||
end
|
end
|
||||||
|
|
||||||
player:tick(ctx)
|
player:tick(ctx)
|
||||||
-- draw_camera{position = Vector3(0, 1, 0), direction = Vector3.FORWARD}
|
|
||||||
-- draw ground
|
|
||||||
local q = util.create_plane_quad(Vector3(0, 0, 0), Vector3.UP, 20)
|
local q = util.create_plane_quad(Vector3(0, 0, 0), Vector3.UP, 20)
|
||||||
local params = {
|
local params = {
|
||||||
texture = "images/measure001a.png",
|
texture = "images/measure001a.png",
|
||||||
|
98
data/scripts/types/list.lua
Normal file
98
data/scripts/types/list.lua
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
---@class List
|
||||||
|
local List = {}
|
||||||
|
|
||||||
|
local function reduce(list, f, init)
|
||||||
|
local acc = init
|
||||||
|
for i, v in ipairs(list) do
|
||||||
|
acc = f(v, acc, i)
|
||||||
|
end
|
||||||
|
return acc
|
||||||
|
end
|
||||||
|
|
||||||
|
local function filter(list, predicate)
|
||||||
|
return reduce(list, function(el, acc, i)
|
||||||
|
if predicate(el) then
|
||||||
|
table.insert(acc, el)
|
||||||
|
end
|
||||||
|
return acc
|
||||||
|
end, List.create{})
|
||||||
|
end
|
||||||
|
|
||||||
|
local function shallow_copy(t)
|
||||||
|
local t2 = {}
|
||||||
|
for k, v in ipairs(t) do
|
||||||
|
t2[k] = v
|
||||||
|
end
|
||||||
|
return t2
|
||||||
|
end
|
||||||
|
|
||||||
|
List.__index = List
|
||||||
|
setmetatable(List, {
|
||||||
|
__call = function(self, ...)
|
||||||
|
local args = {...}
|
||||||
|
if #args == 0 then
|
||||||
|
return List.create()
|
||||||
|
end
|
||||||
|
|
||||||
|
if type(args[1]) == "table" then
|
||||||
|
return List.create(args[1])
|
||||||
|
end
|
||||||
|
return List.create(args)
|
||||||
|
end
|
||||||
|
})
|
||||||
|
|
||||||
|
---Constructs a new list from the given table.
|
||||||
|
---@param from table?
|
||||||
|
---@return List
|
||||||
|
function List.create(from)
|
||||||
|
from = from or {}
|
||||||
|
local l = shallow_copy(from)
|
||||||
|
return setmetatable(l, List)
|
||||||
|
end
|
||||||
|
|
||||||
|
function List:__tostring()
|
||||||
|
local s = "List(" .. table.concat(self, ", ", 1, #self) .. ")"
|
||||||
|
return s
|
||||||
|
end
|
||||||
|
|
||||||
|
---Appends v to the end of the list.
|
||||||
|
---@param v any
|
||||||
|
function List:push(v)
|
||||||
|
table.insert(self, v)
|
||||||
|
end
|
||||||
|
|
||||||
|
---Removes the last element in the list and returns it.
|
||||||
|
---@return any
|
||||||
|
function List:pop()
|
||||||
|
return table.remove(self, #self)
|
||||||
|
end
|
||||||
|
|
||||||
|
---Reduce.
|
||||||
|
---@param f function called with element, accumulator, index
|
||||||
|
---@param init any initial value of accumulator
|
||||||
|
---@return any
|
||||||
|
function List:reduce(f, init)
|
||||||
|
return reduce(self, f, init)
|
||||||
|
end
|
||||||
|
|
||||||
|
---Returns a new List of all elements of this list that match the predicate function.
|
||||||
|
---@param predicate function called with element
|
||||||
|
---@return List
|
||||||
|
function List:filter(predicate)
|
||||||
|
return filter(self, predicate)
|
||||||
|
end
|
||||||
|
|
||||||
|
---Returns the index of value, if it exists in the list, -1 otherwise.
|
||||||
|
---@param value any
|
||||||
|
---@return integer
|
||||||
|
function List:find(value)
|
||||||
|
local idx = -1
|
||||||
|
for i, v in ipairs(self) do
|
||||||
|
if v == value then
|
||||||
|
idx = i
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return idx
|
||||||
|
end
|
||||||
|
return List
|
@ -27,6 +27,25 @@ local function is_weak_vector3(t)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---Coerces t to a Vector3. If t is already a Vector3, returns it without copying.
|
||||||
|
---@param t vectorlike
|
||||||
|
---@param do_err boolean?
|
||||||
|
---@param def Vector3?
|
||||||
|
---@return boolean error, Vector3 result
|
||||||
|
local function coerce(t, do_err, def)
|
||||||
|
do_err = do_err or true
|
||||||
|
if do_err and not is_weak_vector3(t) then
|
||||||
|
def = def or Vector3()
|
||||||
|
error("Vector3: can not coerce t into Vector3. Returning " .. tostring(def))
|
||||||
|
return true, def
|
||||||
|
end
|
||||||
|
if t["_CLASS_"] == "Vector3" then
|
||||||
|
return false, t
|
||||||
|
end
|
||||||
|
|
||||||
|
return false, Vector3(t)
|
||||||
|
end
|
||||||
|
|
||||||
---Returns a Vector3 multiplied either component-wise (if b is a weak Vector3) or multiplies each component by b if b is a number.
|
---Returns a Vector3 multiplied either component-wise (if b is a weak Vector3) or multiplies each component by b if b is a number.
|
||||||
---@param b number|vectorlike
|
---@param b number|vectorlike
|
||||||
---@return Vector3
|
---@return Vector3
|
||||||
@ -167,12 +186,11 @@ end
|
|||||||
---@param with vectorlike
|
---@param with vectorlike
|
||||||
---@return number
|
---@return number
|
||||||
function Vector3:dot(with)
|
function Vector3:dot(with)
|
||||||
if not is_weak_vector3(with) then
|
local err, v2 = coerce(with)
|
||||||
error("Vector3: with must be a Vector3-like table. Returning 0")
|
if err then
|
||||||
return 0
|
return 0.0
|
||||||
end
|
end
|
||||||
|
|
||||||
local v2 = Vector3(with)
|
|
||||||
return self.x * v2.x + self.y * v2.y + self.z * v2.z
|
return self.x * v2.x + self.y * v2.y + self.z * v2.z
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -180,11 +198,10 @@ end
|
|||||||
---@param with vectorlike
|
---@param with vectorlike
|
||||||
---@return Vector3
|
---@return Vector3
|
||||||
function Vector3:cross(with)
|
function Vector3:cross(with)
|
||||||
if not is_weak_vector3(with) then
|
local err, v2 = coerce(with)
|
||||||
error("Vector3: with must be a Vector3-like table. Returning Vector3()")
|
if err then
|
||||||
return Vector3()
|
return v2
|
||||||
end
|
end
|
||||||
local v2 = Vector3(with)
|
|
||||||
return Vector3 {
|
return Vector3 {
|
||||||
self.y * v2.z - self.z * v2.y,
|
self.y * v2.z - self.z * v2.y,
|
||||||
self.z * v2.x - self.x * v2.z,
|
self.z * v2.x - self.x * v2.z,
|
||||||
@ -197,17 +214,22 @@ end
|
|||||||
---@param angle number
|
---@param angle number
|
||||||
---@return Vector3
|
---@return Vector3
|
||||||
function Vector3:rotated(axis, angle)
|
function Vector3:rotated(axis, angle)
|
||||||
if not is_weak_vector3(axis) then
|
-- if not is_weak_vector3(axis) then
|
||||||
error("Vector3: axis must be a Vector3-like table. Returning Vector3()")
|
-- error("Vector3: axis must be a Vector3-like table. Returning Vector3()")
|
||||||
return Vector3()
|
-- return Vector3()
|
||||||
|
-- end
|
||||||
|
local err, vaxis = coerce(axis)
|
||||||
|
if err then
|
||||||
|
return vaxis
|
||||||
end
|
end
|
||||||
|
|
||||||
axis = Vector3(axis):normalized()
|
---@diagnostic disable-next-line: undefined-field
|
||||||
|
vaxis = vaxis:normalized()
|
||||||
local cosa = math.cos(angle)
|
local cosa = math.cos(angle)
|
||||||
local sina = math.sin(angle)
|
local sina = math.sin(angle)
|
||||||
-- __mul is only defined for the left operand (table), numbers don't get metatables.
|
-- __mul is only defined for the left operand (table), numbers don't get metatables.
|
||||||
-- as such, the ordering of operations here is specific
|
-- as such, the ordering of operations here is specific
|
||||||
local v = (self * cosa) + (axis * ((1 - cosa) * self:dot(axis))) + (axis:cross(self) * sina)
|
local v = (self * cosa) + (vaxis * ((1 - cosa) * self:dot(vaxis))) + (vaxis:cross(self) * sina)
|
||||||
return Vector3(v)
|
return Vector3(v)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -222,12 +244,11 @@ end
|
|||||||
---@param t number
|
---@param t number
|
||||||
---@return Vector3
|
---@return Vector3
|
||||||
function Vector3:lerp(b, t)
|
function Vector3:lerp(b, t)
|
||||||
-- return self:copy()
|
local err, w = coerce(b)
|
||||||
if not is_weak_vector3(b) then
|
if err then
|
||||||
error("Vector3: b must be a Vector3-like table. Returning copy of self.")
|
return w
|
||||||
return self:copy()
|
|
||||||
end
|
end
|
||||||
local w = Vector3(b)
|
|
||||||
return Vector3{
|
return Vector3{
|
||||||
util.lerp(self.x, w.x, t),
|
util.lerp(self.x, w.x, t),
|
||||||
util.lerp(self.y, w.y, t),
|
util.lerp(self.y, w.y, t),
|
||||||
@ -235,6 +256,44 @@ function Vector3:lerp(b, t)
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Vector3:direction_to(to)
|
||||||
|
local err, other = coerce(to)
|
||||||
|
if err then
|
||||||
|
return 0.0
|
||||||
|
end
|
||||||
|
|
||||||
|
return Vector3(other.x - self.x, other.y - self.y, other.z - self.z):normalized()
|
||||||
|
end
|
||||||
|
|
||||||
|
---Returns the squared distance between this vector and to.
|
||||||
|
---@param to vectorlike
|
||||||
|
---@return number
|
||||||
|
function Vector3:distance_squared_to(to)
|
||||||
|
local err, other = coerce(to)
|
||||||
|
if err then
|
||||||
|
return 0.0
|
||||||
|
end
|
||||||
|
|
||||||
|
return (other - self):length_squared()
|
||||||
|
end
|
||||||
|
|
||||||
|
---Returns the distance between this vector and to.
|
||||||
|
---@param to vectorlike
|
||||||
|
---@return number
|
||||||
|
function Vector3:distance_to(to)
|
||||||
|
local err, other = coerce(to)
|
||||||
|
if err then
|
||||||
|
return 0.0
|
||||||
|
end
|
||||||
|
|
||||||
|
return (other - self):length()
|
||||||
|
end
|
||||||
|
|
||||||
|
---Returns a new vector with the Y discarded (0).
|
||||||
|
---@return Vector3
|
||||||
|
function Vector3:horizontal()
|
||||||
|
return Vector3(self.x, 0.0, self.z)
|
||||||
|
end
|
||||||
---- CONSTANTS
|
---- CONSTANTS
|
||||||
|
|
||||||
Vector3.UP = Vector3(0, 1, 0)
|
Vector3.UP = Vector3(0, 1, 0)
|
||||||
|
@ -116,5 +116,13 @@ function util.clamp(v, min, max)
|
|||||||
return math.max(math.min(v, max), min)
|
return math.max(math.min(v, max), min)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---returns a random float in range (min, max)
|
||||||
|
---@param min number
|
||||||
|
---@param max number
|
||||||
|
---@return number
|
||||||
|
function util.random_float(min, max)
|
||||||
|
return min + math.random() * (max - min)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
return util
|
return util
|
Loading…
Reference in New Issue
Block a user