NPC Trial #1
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
This commit is contained in:
parent
1c2fb970bb
commit
f17795c911
2 changed files with 130 additions and 29 deletions
158
main.lua
158
main.lua
|
|
@ -1,38 +1,31 @@
|
||||||
|
|
||||||
|
character = {
|
||||||
|
subclasses = {
|
||||||
|
create = {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
functiongenerator = {}
|
||||||
|
check_space = {}
|
||||||
|
|
||||||
function love.load()
|
function love.load()
|
||||||
|
math.randomseed(os.time())
|
||||||
physics = require('physics')
|
physics = require('physics')
|
||||||
window = {}
|
window = {}
|
||||||
|
NPCs = {}
|
||||||
window.x, window.y = love.window.getDesktopDimensions()
|
window.x, window.y = love.window.getDesktopDimensions()
|
||||||
love.window.setMode(window.x, window.y)
|
|
||||||
player = {
|
|
||||||
x = window.x/2-window.x/20,
|
|
||||||
y = window.y/2-window.y/20,
|
|
||||||
width = 20,
|
|
||||||
height = 80,
|
|
||||||
physics = {
|
|
||||||
velocityX = 0,
|
|
||||||
velocityY = 0,
|
|
||||||
airtime = 0,
|
|
||||||
terminalVelocity = 53
|
|
||||||
},
|
|
||||||
bools = {
|
|
||||||
isAirborne = true,
|
|
||||||
isWalking = false,
|
|
||||||
isRunning = false,
|
|
||||||
isFacingRight = true
|
|
||||||
},
|
|
||||||
attributes = {
|
|
||||||
maxRunningSpeed = 2
|
|
||||||
},
|
|
||||||
functions = {}
|
|
||||||
}
|
|
||||||
ground = {
|
ground = {
|
||||||
x = 0,
|
x = 0,
|
||||||
y = window.y,
|
y = window.y,
|
||||||
width = window.x,
|
width = window.x,
|
||||||
height = 1
|
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 = {}
|
mouse = {}
|
||||||
function player.functions.getRunningSpeed(runningStatus) if runningStatus then return player.attributes.maxRunningSpeed*2 else return player.attributes.maxRunningSpeed end end
|
function player.functions.getRunningSpeed(runningStatus) if runningStatus then return player.attributes.maxRunningSpeed*2 else return player.attributes.maxRunningSpeed end end
|
||||||
end
|
end
|
||||||
|
|
@ -41,6 +34,14 @@ function love.update(dt)
|
||||||
input(player, dt)
|
input(player, dt)
|
||||||
gravity(player, dt)
|
gravity(player, dt)
|
||||||
mouvement(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
|
end
|
||||||
|
|
||||||
function love.draw()
|
function love.draw()
|
||||||
|
|
@ -50,10 +51,18 @@ function love.draw()
|
||||||
else
|
else
|
||||||
love.graphics.rectangle("line", player.x-10, player.y-20, 20, 20)
|
love.graphics.rectangle("line", player.x-10, player.y-20, 20, 20)
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
function isTouching(hitbox1, hitbox2)
|
|
||||||
end
|
|
||||||
|
|
||||||
function input(object, dt)
|
function input(object, dt)
|
||||||
object.bools.isWalking = false
|
object.bools.isWalking = false
|
||||||
|
|
@ -62,17 +71,30 @@ function input(object, dt)
|
||||||
if love.keyboard.isDown("lshift") then
|
if love.keyboard.isDown("lshift") then
|
||||||
object.bools.isRunning = true
|
object.bools.isRunning = true
|
||||||
end
|
end
|
||||||
if love.keyboard.isDown("a") and love.keyboard.isDown("d") then
|
if love.keyboard.isDown(keybind.left) and love.keyboard.isDown(keybind.right) then
|
||||||
-- you're stopped now :3c
|
-- you're stopped now :3c
|
||||||
elseif love.keyboard.isDown("a") then
|
elseif love.keyboard.isDown(keybind.left) then
|
||||||
object.bools.isWalking = true
|
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
|
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("d") then
|
elseif love.keyboard.isDown(keybind.right) then
|
||||||
object.bools.isWalking = true
|
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
|
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
|
||||||
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)
|
function love.mousemoved(x, y)
|
||||||
if x > player.x then
|
if x > player.x then
|
||||||
player.bools.isFacingRight = true
|
player.bools.isFacingRight = true
|
||||||
|
|
@ -82,5 +104,83 @@ function love.mousemoved(x, y)
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.keypressed(key)
|
function love.keypressed(key)
|
||||||
if key == "space" then player.physics.velocityY = player.physics.velocityY - 3 end
|
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
|
end
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
main = require('main')
|
||||||
function gravity(object, dt)
|
function gravity(object, dt)
|
||||||
if object.bools.isAirborne == true then
|
if object.bools.isAirborne == true then
|
||||||
object.physics.airtime = object.physics.airtime + dt
|
object.physics.airtime = object.physics.airtime + dt
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue