basic character controller with aiming
This commit is contained in:
parent
a11b91f6bc
commit
d06472f57b
12
src/data/controls.lua
Normal file
12
src/data/controls.lua
Normal file
@ -0,0 +1,12 @@
|
||||
|
||||
return {
|
||||
move_up = "w",
|
||||
move_down = "s",
|
||||
move_left = "a",
|
||||
move_right = "d",
|
||||
|
||||
aim_up = "up",
|
||||
aim_down = "down",
|
||||
aim_left = "left",
|
||||
aim_right = "right",
|
||||
}
|
@ -1,7 +1,48 @@
|
||||
local MainState = {}
|
||||
local pprint = require("lib.pprint")
|
||||
local nata = require("lib.nata")
|
||||
local Vec = require("lib.brinevector")
|
||||
|
||||
function MainState:enter()
|
||||
self.ecs = nata.new{
|
||||
groups = {
|
||||
physical = {filter = {"pos", "vel"}},
|
||||
player = {filter = {"pos", "acc", "speed", "score"}},
|
||||
sprite = {filter = {"sprite"}}
|
||||
},
|
||||
systems = {
|
||||
require("systems.physics"),
|
||||
require("systems.player"),
|
||||
require("systems.sprite"),
|
||||
},
|
||||
data = {}
|
||||
}
|
||||
|
||||
self.ecs:queue{
|
||||
pos = Vec(100, 100),
|
||||
vel = Vec(0, 0),
|
||||
acc = Vec(),
|
||||
score = 0,
|
||||
sprite = {},
|
||||
speed = 100
|
||||
}
|
||||
end
|
||||
|
||||
function MainState:update(dt)
|
||||
self.ecs:flush()
|
||||
self.ecs:emit("update", dt)
|
||||
|
||||
if love.keyboard.isDown("escape") then
|
||||
love.event.quit()
|
||||
end
|
||||
end
|
||||
|
||||
function MainState:mousemoved(...)
|
||||
self.ecs:emit("mousemoved", ...)
|
||||
end
|
||||
|
||||
function MainState:draw()
|
||||
love.graphics.rectangle("fill", 20, 20, 100, 100)
|
||||
self.ecs:emit("draw")
|
||||
end
|
||||
|
||||
return MainState
|
||||
|
13
src/systems/physics.lua
Normal file
13
src/systems/physics.lua
Normal file
@ -0,0 +1,13 @@
|
||||
local Physics = {}
|
||||
|
||||
function Physics:update(dt)
|
||||
for _, e in ipairs(self.pool.groups.physical.entities) do
|
||||
if e.acc then
|
||||
e.vel = e.vel + e.acc * dt
|
||||
end
|
||||
|
||||
e.pos = e.pos + e.vel * dt
|
||||
end
|
||||
end
|
||||
|
||||
return Physics
|
65
src/systems/player.lua
Normal file
65
src/systems/player.lua
Normal file
@ -0,0 +1,65 @@
|
||||
local Player = {}
|
||||
local pprint = require("lib.pprint")
|
||||
local data = require("data")
|
||||
local Vec = require("lib.brinevector")
|
||||
|
||||
local controls = data.controls
|
||||
|
||||
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)
|
||||
local dy = (love.keyboard.isDown(down_key) and 1 or 0)
|
||||
- (love.keyboard.isDown(up_key) and 1 or 0)
|
||||
return Vec(dx, dy).normalized
|
||||
end
|
||||
|
||||
function Player:getMoveDirection()
|
||||
return getDirection(
|
||||
controls.move_up,
|
||||
controls.move_down,
|
||||
controls.move_left,
|
||||
controls.move_right
|
||||
)
|
||||
end
|
||||
|
||||
function Player:getAimDirection(player)
|
||||
if self.use_mouse_aim then
|
||||
return (Vec(love.mouse.getPosition()) - player.pos).normalized
|
||||
else
|
||||
return getDirection(
|
||||
controls.aim_up,
|
||||
controls.aim_down,
|
||||
controls.aim_left,
|
||||
controls.aim_right
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
function Player:update(dt)
|
||||
local move_direction = self:getMoveDirection()
|
||||
|
||||
if love.keyboard.isDown(controls.aim_up, controls.aim_down, controls.aim_left, controls.aim_right) then
|
||||
self.use_mouse_aim = false
|
||||
end
|
||||
|
||||
for _, e in ipairs(self.pool.groups.player.entities) do
|
||||
e.aim_dir = self:getAimDirection(e)
|
||||
e.acc = move_direction * e.speed
|
||||
end
|
||||
end
|
||||
|
||||
function Player:mousemoved()
|
||||
self.use_mouse_aim = true
|
||||
end
|
||||
|
||||
function Player:draw()
|
||||
for _, e in ipairs(self.pool.groups.player.entities) do
|
||||
if e.aim_dir.x ~= 0 or e.aim_dir.y ~= 0 then
|
||||
love.graphics.setColor(0.8, 0.2, 0.2)
|
||||
local aim_pos = e.pos + e.aim_dir * 25
|
||||
love.graphics.circle("fill", aim_pos.x, aim_pos.y, 10)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return Player
|
10
src/systems/sprite.lua
Normal file
10
src/systems/sprite.lua
Normal file
@ -0,0 +1,10 @@
|
||||
local Sprite = {}
|
||||
|
||||
function Sprite:draw()
|
||||
for _, e in ipairs(self.pool.groups.sprite.entities) do
|
||||
love.graphics.setColor(1, 1, 1)
|
||||
love.graphics.circle("fill", e.pos.x, e.pos.y, 20)
|
||||
end
|
||||
end
|
||||
|
||||
return Sprite
|
Loading…
Reference in New Issue
Block a user