reafactor race world

This commit is contained in:
fnicon 2026-03-22 13:05:40 +09:00
parent 85d424f6e5
commit f12f919c63
10 changed files with 403 additions and 13 deletions

View File

@ -0,0 +1,22 @@
local main_wrapper = require "love_src.wrapper.lappy.world"
---@class wrappers.Concord.world : lappy.world
local world = main_wrapper:extend()
local reap = require("lib.reap")
local BASE = reap.base_path(...)
function world:new()
world.super.new(self, BASE, "actor")
end
local default = {
forward = 0.0,
steer = 0.0
}
function world:load(entity, data)
self.e = entity
self.data = data or default
end
return world

View File

@ -0,0 +1,37 @@
local vm = require"lib.vornmath"
local main_wrapper = require "love_src.wrapper.lappy.world"
---@class wrappers.Concord.world : lappy.world
local world = main_wrapper:extend()
local reap = require("lib.reap")
local BASE = reap.base_path(...)
function world:new()
world.super.new(self, BASE, "map")
end
local default = {
drag_movement = 0.0,
friction = 1.0,
drag = vm.vec2(1.0, 0.0)
}
function world:load(entity, data)
self.e = entity
self.data = data or default
end
local function debug_data(x, y, r, g, b, to_debug)
local font_ix = test.draw_font_start()
test.draw_font_set_base_color(r, g, b)
for k, v in pairs(to_debug) do
y = y + test.draw_font(font_ix, string.format("%s : %s", k, v), x, y)
end
end
function world:ui_draw()
debug_data(650, 800, 1.0, 0.0, 1.0, self.data)
end
return world

View File

@ -0,0 +1,51 @@
local main_wrapper = require "love_src.wrapper.lappy.world"
---@class wrappers.Concord.world : lappy.world
local world = main_wrapper:extend()
local reap = require("lib.reap")
local BASE = reap.base_path(...)
function world:new()
world.super.new(self, BASE, "player")
end
local function handle_input(accel, brake, steer)
local up = love.keyboard.isDown("up")
local down = love.keyboard.isDown("down")
local left = love.keyboard.isDown("left")
local right = love.keyboard.isDown("right")
local desire_forward = 0
local desire_steer = 0
if (up) then
desire_forward = accel
elseif (down) then
desire_forward = - brake
end
if (left) then
desire_steer = steer
elseif (right) then
desire_steer = - steer
end
return desire_forward, desire_steer
end
local default = {
forward = 0.0,
steer = 0.0
}
function world:load(entity, data)
self.e = entity
self.data = data or default
end
function world:update(dt)
local accel, brake, steer = self.e.components.racer.data.accel, self.e.components.racer.data.brake, self.e.components.racer.data.steer
local desire_forward, desire_steer = handle_input(accel, brake, steer)
self.data.forward = desire_forward
self.data.steer = desire_steer
end
return world

View File

@ -0,0 +1,137 @@
local vm = require"lib.vornmath"
local racing_force = require("love_src.src.system.racing_force")
local main_wrapper = require "love_src.wrapper.lappy.world"
---@class wrappers.Concord.world : lappy.world
local world = main_wrapper:extend()
local reap = require("lib.reap")
local BASE = reap.base_path(...)
function world:new()
world.super.new(self, BASE, "racer")
end
local default = {
pos = vm.vec2(44.64, 10.87),
orientation = vm.vec2(0.0, 0.0),
velocity = vm.vec2(0.0, 0.0),
max_speed = 1.0,
min_speed = -1.0,
inertia = vm.vec2(0.0, 0.0),
accel = 0.1,
brake = 0.1,
grip = 0.0,
steer = 0.1,
mass = 0.0,
streamline = 0.0
}
local magic_w = {
tire = 0.0,
floor = 0.0,
forward = 0.01,
steer = 0.1,
drag_boost = 0.0,
drag_halt = 0.0,
inertia = 0.0,
centrifugal = 0.0,
}
local debug_index = 1
function world:load(entity, data)
self.e = entity
self.data = data or default
self.map = {}
end
function get(s)
return
s.data.max_speed,
s.data.min_speed,
s.data.velocity,
s.data.accel,
s.data.brake,
s.data.grip,
s.data.steer,
s.data.inertia,
s.data.mass,
s.data.streamline
end
function world:update_race(dt)
local x, y = self.data.pos[1], self.data.pos[2]
local or_x, or_y = self.data.orientation[1], self.data.orientation[2]
local max_speed, min_speed, velocity, accel, brake, grip, steer, inertia, mass, streamline = get(self)
local friction, drag, drag_movement = self.map.data.friction, self.map.data.drag, self.map.data.drag_movement
local desire_forward, desire_steer = self.e.components.actor.data.forward, self.e.components.actor.data.steer
local force = racing_force(
desire_forward, desire_steer,
grip, friction,
drag, drag_movement, streamline,
inertia,
mass,
magic_w.forward, magic_w.steer,
magic_w.tire, magic_w.floor,
magic_w.drag_boost, magic_w.drag_halt,
magic_w.inertia, magic_w.centrifugal
)
if (mass == 0) then
mass = 1
end
local a = force * dt / mass
self.data.velocity = self.data.velocity + a
self.data.velocity[1] = vm.clamp(self.data.velocity[1], min_speed, max_speed)
-- if (self.data.velocity[1] ~= 0) then
self.data.pos = self.data.pos + self.data.velocity
-- end
self.data.inertia = force
end
function world:update(dt)
self:update_race(dt)
end
function world:draw()
local x, y = self.data.pos[1], self.data.pos[2]
local or_x, or_y = self.data.orientation[1], self.data.orientation[2]
local vx, vy = self.data.velocity[1], self.data.velocity[2]
test.set_sphere(x, y, vx, vy)
end
local function debug_data(x, y, r, g, b, to_debug)
local font_ix = test.draw_font_start()
test.draw_font_set_base_color(r, g, b)
for k, v in pairs(to_debug) do
y = y + test.draw_font(font_ix, string.format("%s : %s", k, v), x, y)
end
end
local function debug_vector(vx, vy)
local lw, lh = love.graphics.getDimensions()
test.draw_line_quad_start()
test.draw_set_color(1.0, 0.0, 0.0) -- r, g, b (0.0 to 1.0)
test.draw_line(lw/2, lh/2, lw/vx, lh/vy) -- x1, y1, x2, y2
end
function world:ui_draw()
debug_data(650, 0, 1.0, 0.0, 1.0, self.data)
debug_data(650, 400, 1.0, 0.0, 1.0, magic_w)
local vx, vy = self.data.velocity[1], self.data.velocity[2]
debug_vector(vx, vy)
end
return world

View File

@ -0,0 +1,127 @@
local vm = require"lib.vornmath"
local main_wrapper = require "love_src.wrapper.lappy.world"
---@class wrappers.Concord.world : lappy.world
local world = main_wrapper:extend()
local reap = require("lib.reap")
local BASE = reap.base_path(...)
local default = {
racers = {
base = require "love_src.src.new.components.racer"
}
}
local entities_default = {
{
components = {
racer = require("love_src.src.new.components.racer"),
actor = require("love_src.src.new.components.player"),
},
type = "racer",
name = "racer 1",
data = {
racer = {
pos = vm.vec2(44.64, 10.87),
orientation = vm.vec2(0.0, 0.0),
velocity = vm.vec2(0.0, 0.0),
max_speed = 1.0,
min_speed = -1.0,
inertia = vm.vec2(0.0, 0.0),
accel = 0.1,
brake = 0.1,
grip = 0.0,
steer = 0.1,
mass = 0.0,
streamline = 0.0
},
actor = {
forward = 0.0,
steer = 0.0
}
}
},
{
components = {
map = require"love_src.src.new.components.map"
},
type = "map",
name = "map 1",
data = {
map = {
drag_movement = 0.0,
friction = 1.0,
drag = vm.vec2(1.0, 0.0)
}
}
}
}
function world:new()
world.super.new(self, BASE, "race")
self.is_loaded = false
self.entities = {}
end
function world:load(data, entities_data)
self.data = data or default
self.is_loaded = true
self.entities_data = entities_data or entities_default
local map
for _, d in pairs(self.entities_data) do
local e = {
type = d.type,
name = d.name,
components = {}
}
for t, c in pairs(d.components) do
e.components[t] = c()
e.components[t]:load(e, d.data[t])
end
table.insert(self.entities, e)
if (e.type == "map") then
map = e
end
end
for _, e in ipairs(self.entities) do
if (e.type == "racer") then
for t, c in pairs(e.components) do
if (c.name == "racer") then
c.map = map.components.map
end
end
end
end
end
function world:update(dt)
for _, e in ipairs(self.entities) do
for t, c in pairs(e.components) do
c:update(dt)
end
end
end
function world:draw()
for _, e in ipairs(self.entities) do
for t, c in pairs(e.components) do
c:draw()
end
end
end
function world:ui_draw()
for _, e in ipairs(self.entities) do
for t, c in pairs(e.components) do
c:ui_draw()
end
end
end
return world

View File

@ -3,6 +3,7 @@ local vm = require("lib.vornmath")
local components = {}
components.dict = {
forward = "race.forward",
velocity = "race.velocity",
max_speed = "race.max_speed",
min_speed = "race.min_speed",
@ -15,6 +16,10 @@ components.dict = {
streamline = "race.streamline",
}
function components.forward (c, x, y)
c.data = vm.vec2(x, y)
end
function components.velocity (c, x, y)
c.data = vm.vec2(x, y)
end

View File

@ -32,6 +32,7 @@ function wrapper:load(_args)
assemblage = racer.assemble,
data = {
velocity = {0.0, 0.0},
forward = {0.0, 0.0},
max_speed = 1.0,
min_speed = 0.0,
inertia = {0.0, 0.0},

View File

@ -16,6 +16,7 @@ system.pool = {
race.dict.max_speed,
race.dict.min_speed,
race.dict.velocity,
race.dict.forward,
race.dict.accel,
race.dict.brake,
race.dict.grip,
@ -30,6 +31,7 @@ system.components = {
[race.dict.max_speed] = race.max_speed,
[race.dict.min_speed] = race.min_speed,
[race.dict.velocity] = race.velocity,
[race.dict.forward] = race.forward,
[race.dict.accel] = race.accel,
[race.dict.brake] = race.brake,
@ -57,6 +59,7 @@ local function get(e)
e[race.dict.max_speed].data,
e[race.dict.min_speed].data,
e[race.dict.velocity].data,
e[race.dict.forward].data,
e[race.dict.accel].data,
e[race.dict.brake].data,
e[race.dict.grip].data,
@ -184,15 +187,18 @@ dict_debugs = {
race.dict.max_speed,
race.dict.min_speed,
race.dict.velocity,
race.dict.forward,
race.dict.accel,
race.dict.brake,
race.dict.grip,
race.dict.steer,
race.dict.inertia,
race.dict.mass,
race.dict.streamline
race.dict.streamline,
}
-- local function
function system:update(dt)
local i_up = love.keyboard.isDown("i")
local i_down = love.keyboard.isDown("k")
@ -222,7 +228,7 @@ function system:update(dt)
e[dict_debug_current].data = e[dict_debug_current].data + 1
end
local max_speed, min_speed, velocity, accel, brake, grip, steer, inertia, mass, streamline = get(e)
local max_speed, min_speed, velocity, forward, accel, brake, grip, steer, inertia, mass, streamline = get(e)
local pos_x, pos_y = e["race.pos"].data[1], e["race.pos"].data[2]
@ -250,7 +256,7 @@ function system:update(dt)
e[race.dict.velocity].data = e[race.dict.velocity].data + a
e[race.dict.velocity].data[1] = vm.clamp(e[race.dict.velocity].data[1], min_speed, max_speed)
if (desire_forward ~= 0) then
if (e[race.dict.velocity].data[1] ~= 0) then
e["race.pos"].data = e["race.pos"].data + e[race.dict.velocity].data
end
@ -277,15 +283,16 @@ end
function system:ui_draw()
for _, e in ipairs(self.pool) do
local max_speed, min_speed, velocity, accel, brake, grip, steer, inertia, mass, streamline = get(e)
local max_speed, min_speed, velocity, forward, accel, brake, grip, steer, inertia, mass, streamline = get(e)
test.draw_line_quad_start()
test.draw_set_color(1.0, 0.0, 0.0) -- r, g, b (0.0 to 1.0)
test.draw_line(100, 100, velocity[1] * 100, velocity[2]* 100) -- x1, y1, x2, y2
test.draw_line(lw/2, lh/2, velocity[1], velocity[2]) -- x1, y1, x2, y2
draw_debug(640, 0, 1.0, 0.0, 1.0, {
string.format("%s max_speed : %s", (debug_current == 1 and ">") or "", max_speed),
string.format("%s max_speed : %s", (debug_current == 21 and ">") or "", min_speed),
string.format("%s velocity : %s", (debug_current == 2 and ">") or "", velocity),
string.format("%s forward : %s", (debug_current == 22 and ">") or "", forward),
string.format("%s accel : %s", (debug_current == 3 and ">") or "", accel),
string.format("%s brake : %s", (debug_current == 4 and ">") or "", brake),
string.format("%s grip : %s", (debug_current == 5 and ">") or "", grip),
@ -307,7 +314,7 @@ function system:ui_draw()
string.format("%s magic drag movement : %s", (debug_current == 18 and ">") or "", magic_w.drag_movement)
})
draw_debug(1000, 0, 1.0, 0.0, 1.0, {
draw_debug(640,600, 1.0, 0.0, 1.0, {
string.format("%s friction : %s", (debug_current == 19 and ">") or "", frictions[1].friction),
string.format("%s drag : %s", (debug_current == 20 and ">") or "", drags[1].drag),
})

View File

@ -4,6 +4,7 @@ local template = {}
template.default_data = {
velocity = {0.0, 0.0},
forward = {0.0, 0.0},
max_speed = 10.0,
min_speed = 0.0,
@ -19,6 +20,7 @@ function template.assemble(e, data)
e:give(race.dict.max_speed, data.max_speed)
e:give(race.dict.min_speed, data.min_speed)
e:give(race.dict.velocity, data.velocity[1], data.velocity[2])
e:give(race.dict.velocity, data.forward[1], data.forward[2])
e:give(race.dict.accel, data.accel)
e:give(race.dict.brake, data.brake)

View File

@ -191,15 +191,16 @@ end
local wm = require("world_map")
world = {
["top_down_race"] = require("love_src.src.world.top_down_race")(),
["main_menu"] = require("love_src.src.world.main_menu")(),
["1_intro"] = require("love_src.src.world.1_intro")(),
["2_town_square"] = require("love_src.src.world.2_town_square")(),
["race"] = require("love_src.src.world.race")(),
["train"] = require("love_src.src.world.train")(),
["race"] = require("love_src.src.new.world.race")()
-- ["top_down_race"] = require("love_src.src.world.top_down_race")(),
-- ["main_menu"] = require("love_src.src.world.main_menu")(),
-- ["1_intro"] = require("love_src.src.world.1_intro")(),
-- ["2_town_square"] = require("love_src.src.world.2_town_square")(),
-- ["race"] = require("love_src.src.world.race")(),
-- ["train"] = require("love_src.src.world.train")(),
};
current = wm["top_down_race"]
current = wm["race"]
function load_world(world_to_load)
current = world_to_load