-- Define the Player object (because players don't do it themselves)
local Player = {}
Player.__index = Player
-- Create a new Player (because the universe needs more adventurers)
function Player.new(name)
local self = setmetatable({}, Player)
self.name = name or "Unnamed Adventurer"
self.position = {x = 0, y = 0, z = 0}
self.speed = math.random(5, 10) -- Speed is overrated, but let's keep it fun
return self
end
-- Move the player (because standing still is boring)
function Player:move(direction)
local dx = math.sin(direction) * self.speed
local dz = math.cos(direction) * self.speed
self.position.x = self.position.x + dx
self.position.z = self.position.z + dz
print(self.name .. " moved to [" .. string.format("%.2f", self.position.x) .. ", " .. string.format("%.2f", self.position.z) .. "]")
end
-- Player jump, because sometimes we need to defy gravity
function Player:jump()
local jumpHeight = math.random(1, 5)
self.position.y = self.position.y + jumpHeight
print(self.name .. " jumped " .. jumpHeight .. " units high!")
end
-- Example of a new Player doing heroic stuff (or just walking around)
local player1 = Player.new("CodeWarrior")
player1:move(math.pi / 3) -- Moving at an impressive angle
player1:jump() -- Up, up and... slightly up
-- Whats next?