Removed initial player creation, replaced with a function that creates a character. Added ability to generate NPCs by pressing [Interact Key] Added key binds Added NPC wandering and ability to say hello upon interaction
186 lines
No EOL
5.6 KiB
Lua
186 lines
No EOL
5.6 KiB
Lua
|
|
character = {
|
|
subclasses = {
|
|
create = {}
|
|
}
|
|
}
|
|
functiongenerator = {}
|
|
check_space = {}
|
|
|
|
function love.load()
|
|
math.randomseed(os.time())
|
|
physics = require('physics')
|
|
window = {}
|
|
NPCs = {}
|
|
window.x, window.y = love.window.getDesktopDimensions()
|
|
ground = {
|
|
x = 0,
|
|
y = window.y,
|
|
width = window.x,
|
|
height = 1
|
|
}
|
|
love.window.setMode(window.x, window.y)
|
|
defaulkeybinds()
|
|
player = character.create({"physics", "bools", "attributes", "functions"})
|
|
player.x = window.x/2-window.x/20
|
|
player.y = window.y/2-window.y/20
|
|
player.width = 20
|
|
player.height = 80
|
|
mouse = {}
|
|
function player.functions.getRunningSpeed(runningStatus) if runningStatus then return player.attributes.maxRunningSpeed*2 else return player.attributes.maxRunningSpeed end end
|
|
end
|
|
|
|
function love.update(dt)
|
|
input(player, dt)
|
|
gravity(player, dt)
|
|
mouvement(player, dt)
|
|
for i = 1, #NPCs do
|
|
gravity(NPCs[i], dt)
|
|
wander(NPCs[i], dt)
|
|
mouvement(NPCs[i], dt)
|
|
if NPCs[i].bools.isInteracting then
|
|
sayHello(NPCs[i], dt)
|
|
end
|
|
end
|
|
end
|
|
|
|
function love.draw()
|
|
love.graphics.rectangle("line", player.x, player.y, player.width, player.height)
|
|
if player.bools.isFacingRight then
|
|
love.graphics.rectangle("line", player.x+10, player.y-20, 20, 20)
|
|
else
|
|
love.graphics.rectangle("line", player.x-10, player.y-20, 20, 20)
|
|
end
|
|
for i = 1, #NPCs do
|
|
love.graphics.rectangle("fill", NPCs[i].x, NPCs[i].y, NPCs[i].width, NPCs[i].height)
|
|
end
|
|
for i = 1, #NPCs do
|
|
if NPCs[i].bools.isInteracting then
|
|
local font = love.graphics.getFont()
|
|
local helloText = love.graphics.newText(font, "Hello there")
|
|
love.graphics.draw(helloText, NPCs[i].x, NPCs[i].y-10)
|
|
end
|
|
end
|
|
end
|
|
|
|
|
|
function input(object, dt)
|
|
object.bools.isWalking = false
|
|
object.bools.isRunning = false
|
|
mouse.x, mouse.y = love.mouse.getPosition
|
|
if love.keyboard.isDown("lshift") then
|
|
object.bools.isRunning = true
|
|
end
|
|
if love.keyboard.isDown(keybind.left) and love.keyboard.isDown(keybind.right) then
|
|
-- you're stopped now :3c
|
|
elseif love.keyboard.isDown(keybind.left) then
|
|
object.bools.isWalking = true
|
|
if math.abs(object.physics.velocityX) + 0.1 < object.functions.getRunningSpeed(object.bools.isRunning) then object.physics.velocityX = object.physics.velocityX - 0.1 else object.physics.velocityX = object.functions.getRunningSpeed(object.bools.isRunning)* -1 end
|
|
elseif love.keyboard.isDown(keybind.right) then
|
|
object.bools.isWalking = true
|
|
if math.abs(object.physics.velocityX) < object.functions.getRunningSpeed(object.bools.isRunning) then object.physics.velocityX = object.physics.velocityX + 0.1 else object.physics.velocityX = object.functions.getRunningSpeed(object.bools.isRunning) end
|
|
end
|
|
end
|
|
|
|
function wander(object, dt)
|
|
if object.x > object.attributes.wanderpoint + 2 or object.x < object.attributes.wanderpoint - 2 then
|
|
object.bools.isWalking = true
|
|
if object.x < object.attributes.wanderpoint then
|
|
object.physics.velocityX = 1
|
|
else
|
|
object.physics.velocityX = -1
|
|
end
|
|
else
|
|
object.attributes.wanderpoint = math.random(0, window.x)
|
|
end
|
|
end
|
|
|
|
function love.mousemoved(x, y)
|
|
if x > player.x then
|
|
player.bools.isFacingRight = true
|
|
else
|
|
player.bools.isFacingRight = false
|
|
end
|
|
end
|
|
|
|
function love.keypressed(key)
|
|
if key == keybind.jump then player.physics.velocityY = player.physics.velocityY - 3 end
|
|
if key == keybind.interact then
|
|
check_space.interactables(player.x, player.y)
|
|
end
|
|
if key == keybind.create then
|
|
NPCs[#NPCs+1] = character.create({"attributes", "functions","bools","physics"}, false)
|
|
NPCs[#NPCs].x = math.random(0, window.x-20)
|
|
NPCs[#NPCs].y = window.y/2-window.y/20
|
|
NPCs[#NPCs].width = 20
|
|
NPCs[#NPCs].height = 80
|
|
end
|
|
end
|
|
|
|
function sayHello(object, dt)
|
|
object.attributes.time_spoken = object.attributes.time_spoken + dt
|
|
if object.attributes.time_spoken > 2 then
|
|
object.bools.isInteracting = false
|
|
object.attributes.time_spoken = 0
|
|
end
|
|
end
|
|
|
|
function check_space.interactables(x, y)
|
|
for i = 1, #NPCs do
|
|
if x > NPCs[i].x -50 and x < NPCs[i].x +50 then
|
|
NPCs[i].bools.isInteracting = true
|
|
end
|
|
end
|
|
end
|
|
|
|
function defaulkeybinds()
|
|
keybind = {}
|
|
keybind.left = "a"
|
|
keybind.right = "d"
|
|
keybind.jump = "space"
|
|
keybind.interact = "e"
|
|
keybind.run = "lshift"
|
|
keybind.create = "f"
|
|
end
|
|
|
|
function character.create(chosenSubclasses, isPlayerControlled)
|
|
local object = {}
|
|
|
|
for i = 1, #chosenSubclasses do
|
|
local current_subclass = chosenSubclasses[i]
|
|
character.subclasses.create[current_subclass](object)
|
|
end
|
|
|
|
if isPlayerControlled == false then
|
|
object.attributes.wanderpoint = math.random(0, window.x)
|
|
object.bools.isInteracting = false
|
|
object.attributes.time_spoken = 0
|
|
end
|
|
|
|
return object
|
|
end
|
|
|
|
function character.subclasses.create.physics(object)
|
|
object.physics = {
|
|
velocityX = 0,
|
|
velocityY = 0,
|
|
airtime = 0,
|
|
terminalVelocity = 53
|
|
}
|
|
end
|
|
function character.subclasses.create.bools(object)
|
|
object.bools = {
|
|
isAirborne = true,
|
|
isWalking = false,
|
|
isRunning = false,
|
|
isFacingRight = true
|
|
}
|
|
end
|
|
function character.subclasses.create.attributes(object)
|
|
object.attributes = {
|
|
maxRunningSpeed = 2
|
|
}
|
|
end
|
|
function character.subclasses.create.functions(object)
|
|
object.functions = {}
|
|
end |