HorseSeaHorse/main.lua

505 lines
12 KiB
Lua
Raw Normal View History

2021-02-05 20:58:58 -06:00
-- pls set tabs to width of 4 spaces
class = require "lib/middleclass"
wind = require "lib/windfield"
stalker = require "lib/STALKER-X"
startSwordLength = 40; startKnifeLength = 10
2021-02-05 20:58:58 -06:00
-- GAME STATES
--------------------------------------------------------------------------------
-- LOVE
----------------------------------------
function love.load ()
math.randomseed(os.time())
love.graphics.setDefaultFilter("nearest", "nearest")
a_ttf = love.graphics.newFont("art/font/alagard.ttf", nil, "none")
r_ttf = love.graphics.newFont("art/font/romulus.ttf", nil, "none")
camera = stalker()
2021-02-06 00:50:51 -06:00
mainmenu_load()
2021-02-05 20:58:58 -06:00
end
function love.update(dt)
2021-02-06 13:17:16 -06:00
updateFunction(dt)
2021-02-05 20:58:58 -06:00
camera:update(dt)
end
function love.draw()
camera:attach()
2021-02-06 13:17:16 -06:00
drawFunction()
2021-02-05 20:58:58 -06:00
camera:detach()
camera:draw()
end
function love.resize()
camera = stalker()
end
function love.keypressed(key)
2021-02-06 13:17:16 -06:00
keypressedFunction(key)
2021-02-05 20:58:58 -06:00
end
function love.keyreleased (key)
2021-02-06 13:17:16 -06:00
keyreleasedFunction(key)
2021-02-05 20:58:58 -06:00
end
2021-02-06 13:37:41 -06:00
-- MAIN-MENU
2021-02-05 20:58:58 -06:00
----------------------------------------
function mainmenu_load ()
2021-02-06 13:37:41 -06:00
mainMenu = makeMainMenu()
2021-02-05 20:58:58 -06:00
2021-02-06 13:37:41 -06:00
updateFunction = function (dt) mainMenu:update(dt) end
drawFunction = function () mainMenu:draw() end
keypressedFunction = function(key) mainMenu:keypressed(key) end
keyreleasedFunction = function(key) mainMenu:keyreleased(key) end
2021-02-05 20:58:58 -06:00
camera = stalker()
end
2021-02-06 13:17:16 -06:00
function makeMainMenu()
return Menu:new(100, 100, 30, 50, 3, {
2021-02-06 00:50:51 -06:00
{love.graphics.newText(a_ttf, "Local"),
2021-02-06 13:17:16 -06:00
function ()
2021-02-06 13:10:15 -06:00
local lobbiest1 = LocalLobbiest:new(nil, KEYMAPS[1])
local lobbiest2 = LocalLobbiest:new(nil, KEYMAPS[2])
game_load({lobbiest1, lobbiest2})
end},
2021-02-05 20:58:58 -06:00
{love.graphics.newText(a_ttf, "Quit"),
function () love.event.quit(0) end }})
end
2021-02-06 13:37:41 -06:00
-- IN-GAME
2021-02-05 20:58:58 -06:00
----------------------------------------
2021-02-06 13:37:41 -06:00
Game = class("Game")
2021-02-05 20:58:58 -06:00
2021-02-06 13:37:41 -06:00
function game_load(lobbiests)
game = Game:new(lobbiests)
2021-02-05 20:58:58 -06:00
2021-02-06 13:37:41 -06:00
updateFunction = function (dt) game:update(dt) end
drawFunction = function () game:draw() end
keypressedFunction = function(key) game:keypressed(key) end
keyreleasedFunction = function(key) game:keyreleased(key) end
2021-02-05 20:58:58 -06:00
end
-- CLASSES
--------------------------------------------------------------------------------
2021-02-06 00:50:51 -06:00
-- Fighter player class
2021-02-05 20:58:58 -06:00
----------------------------------------
Fighter = class('Fighter')
2021-02-06 13:37:41 -06:00
function Fighter:initialize(game, x, y, character, swordType, swordSide)
self.game = game
self.swordType = swordType or 'normal'
self.swordSide = swordSide or 'top'
self.character = character or "jellyfish-lion.png"
2021-02-05 20:58:58 -06:00
self.directionals = {}
self.deadPieces = {}
2021-02-06 00:50:51 -06:00
self.sprite = love.graphics.newImage("art/sprites/" .. self.character)
self:initBody(x, y)
self:initSword()
self:initShield()
end
function Fighter:update(dt)
local dir = self.directionals
self:movement()
self:glueSwordAndShield()
if (dir['left'] == 2 and dir['right'] == 0) then dir['left'] = 1; end
if (dir['right'] == 2 and dir['left'] == 0) then dir['right'] = 1; end
end
function Fighter:draw ()
local x,y = self.body:getWorldPoints(self.body.shape:getPoints())
love.graphics.draw(self.sprite, x, y, self.body:getAngle(), 1, 1)
end
function Fighter:movement ()
end
function Fighter:initBody(x, y)
2021-02-06 13:37:41 -06:00
self.body = self.game.world:newRectangleCollider(x, y, 16, 16);
2021-02-05 20:58:58 -06:00
self.body:setCollisionClass('Fighter')
self.body:setObject(self)
2021-02-06 00:50:51 -06:00
self.body:setAngularDamping(2)
self.body:setLinearDamping(.5)
self.body:setPostSolve(self.makePostSolve())
end
2021-02-06 00:50:51 -06:00
function Fighter:initShield()
2021-02-06 13:37:41 -06:00
self.shield = self.game.world:newRectangleCollider(0, 0, 20, 5);
2021-02-06 00:50:51 -06:00
self.shield:setCollisionClass('Shield')
self.shield:setObject(self)
end
function Fighter:initSword()
self.swordLength = startSwordLength
2021-02-06 00:50:51 -06:00
if (self.swordType == 'normal') then
2021-02-06 13:37:41 -06:00
self.sword = self.game.world:newRectangleCollider(0, 0, 3, self.swordLength);
2021-02-06 00:50:51 -06:00
else
2021-02-06 13:37:41 -06:00
self.sword = self.game.world:newRectangleCollider(0, 0, 3, startKnifeLength);
2021-02-06 00:50:51 -06:00
end
self.sword:setCollisionClass('Sword')
self.sword:setObject(self)
self.sword:setPostSolve(self:makeSwordPostSolve())
end
function Fighter:glueSwordAndShield ()
local x, y = self.body:getPosition()
local angle = self.body:getAngle()
self.shield:setAngle(angle)
self.shield:setPosition(x - (math.sin(angle) * 16),
y + (math.cos(angle) * 16))
if (self.swordType == 'normal') then
local offset = self.swordLength - 10
self.sword:setAngle(angle)
self.sword:setPosition(x + (math.sin(angle) * offset),
y - (math.cos(angle) * offset))
elseif (self.swordType == 'knife') then
local offset = self.swordLength * 2
self.sword:setAngle(angle)
self.sword:setPosition(x + (math.sin(angle) * 4.5 * offset),
y - (math.cos(angle) * 1.5 * offset))
end
2021-02-05 20:58:58 -06:00
end
2021-02-06 00:50:51 -06:00
function Fighter:makePostSolve()
return function(col1, col2, contact)
if (col1.collision_class == "Fighter"
and col2.collision_class == "Sword")
then
-- print(col2.shape)
-- print("THEY DEEED, dude")
2021-02-06 00:50:51 -06:00
end
end
end
2021-02-05 20:58:58 -06:00
2021-02-06 00:50:51 -06:00
function Fighter:makeSwordPostSolve()
return function(col1, col2, contact)
if (col1.collision_class == "Sword"
and col2.collision_class == "Shield")
then
-- print("SWORD CLASH!!!!")
2021-02-06 00:50:51 -06:00
end
2021-02-05 20:58:58 -06:00
end
2021-02-06 00:50:51 -06:00
end
-- LocalPlayer for local players (ofc)
----------------------------------------
LocalPlayer = class("LocalPlayer", Fighter)
2021-02-05 20:58:58 -06:00
2021-02-06 13:37:41 -06:00
function LocalPlayer:initialize(game, x, y, keymap, character, swordType, swordSide)
2021-02-06 13:10:15 -06:00
self.keymap = keymap or KEYMAPS[1]
2021-02-06 13:37:41 -06:00
Fighter.initialize(self, game, x, y, character, swordType, swordSide)
2021-02-06 00:50:51 -06:00
end
function LocalPlayer:movement ()
2021-02-06 00:50:51 -06:00
local x, y = self.body:getPosition()
2021-02-05 20:58:58 -06:00
local dir = self.directionals
2021-02-06 00:50:51 -06:00
local angle = self.body:getAngle()
2021-02-05 20:58:58 -06:00
if (dir['left'] == 1) then
2021-02-06 00:50:51 -06:00
self.body:applyAngularImpulse(-.5, 1)
2021-02-05 20:58:58 -06:00
elseif (dir['right'] == 1) then
2021-02-06 00:50:51 -06:00
self.body:applyAngularImpulse(.5, 1)
2021-02-05 20:58:58 -06:00
end
2021-02-06 00:50:51 -06:00
if (dir['up'] == 1) then
self.body:applyLinearImpulse(math.sin(angle) * 0.5, math.cos(angle) * -0.5)
elseif (dir['down'] == 1) then
self.body:setAngle(angle - (math.pi * .70))
self.body:applyAngularImpulse(-45, 1)
dir['down'] = 0
2021-02-05 20:58:58 -06:00
end
end
2021-02-05 20:58:58 -06:00
2021-02-06 00:50:51 -06:00
function LocalPlayer:keypressed(key)
local dir = self.directionals
if (key == self.keymap["right"]) then
dir['right'] = 1
if (dir['left'] == 1) then dir['left'] = 2; end
elseif (key == self.keymap["left"]) then
dir['left'] = 1
if (dir['right'] == 1) then dir['right'] = 2; end
elseif (key == self.keymap["accel"]) then
dir['up'] = 1
if (dir['down'] == 1) then dir['down'] = 2; end
elseif (key == self.keymap["flip"]) then
dir['down'] = 1
if (dir['up'] == 1) then dir['up'] = 2; end
end
end
function LocalPlayer:keyreleased(key)
local dir = self.directionals
if (key == self.keymap["right"]) then
dir['right'] = 0
elseif (key == self.keymap["left"]) then
dir['left'] = 0
elseif (key == self.keymap["accel"]) then
dir['up'] = 0
elseif (key == self.keymap["flip"]) then
dir['down'] = 0
2021-02-06 00:50:51 -06:00
end
2021-02-05 20:58:58 -06:00
end
-- LocalBot andddd for bots too
----------------------------------------
LocalBot = class("LocalBot", Fighter)
function LocalBot:initialize(x, y, character, swordType, swordSide)
Fighter.initialize(self, x, y, character, swordType, swordSide)
end
2021-02-05 20:58:58 -06:00
2021-02-06 13:37:41 -06:00
-- GAME superclass for matches
----------------------------------------
function Game:initialize(lobbiests)
self.world = wind.newWorld(0, 0, true)
self.world:addCollisionClass('Fighter')
self.world:addCollisionClass('Shield')
self.world:addCollisionClass('Sword')
self.remotePlayers = {}
self.localPlayers = {}
self.remotePlayersN = 0
self.localPlayersN = 0
for k,lobbiest in pairs(lobbiests) do
if (lobbiest.class.name == "LocalLobbiest") then
local i = self.localPlayersN + 1
self.localPlayers[i] = LocalPlayer:new(self, 0 + i * 50, 0 + i * 50, KEYMAPS[i])
self.localPlayersN = i
end
end
self.localFighters = self.localPlayers
self.fighters = self.localFighters
camera:fade(.2, {0,0,0,0})
camera:setFollowLerp(0.1)
camera:setFollowStyle('TOPDOWN')
end
function Game:update(dt)
self.world:update(dt)
for i, fighter in pairs(self.fighters) do
fighter:update(dt)
end
-- local x, y = player.body:getPosition()
-- camera:follow(x, y)
end
function Game:draw ()
self.world:draw()
for i, fighter in pairs(self.fighters) do
fighter:draw()
end
end
function Game:keypressed(key)
local dir = self.localFighters[1].directionals
-- if a player presses the left key, then holds the right key, they should
-- go right until they let go, then they should go left.
if (key == "=" and camera.scale < 10) then
camera.scale = camera.scale + .5
elseif (key == "-" and camera.scale > .5) then
camera.scale = camera.scale - .5
elseif (key == "escape") then
pause_load()
else
for i, player in pairs(self.localPlayers) do
player:keypressed(key)
end
end
end
function Game:keyreleased (key)
for i, player in pairs(self.localPlayers) do
player:keyreleased(key)
end
end
-- LOBBY superclass for pre-matches
----------------------------------------
Lobby = class("Lobby")
-- LOBBIEST proposed fighter
----------------------------------------
Lobbiest = class("Lobbiest")
function Lobbiest:initialize(name)
self.name = name or NAMES[math.random(1, table.maxn(NAMES))]
self.character = CHARACTERS[math.random(1, table.maxn(CHARACTERS))]
end
-- LOCALLOBBIEST
----------------------------------------
LocalLobbiest = class("LocalLobbiest", Lobbiest)
function LocalLobbiest:initialize(name, keymap)
Lobbiest.initialize(self, name)
self.keymap = keymap or KEYMAPS[math.random(1, table.maxn(KEYMAPS))]
end
2021-02-05 20:58:58 -06:00
-- MENU used for creating menus (lol)
----------------------------------------
Menu = class("Menu")
function Menu:initialize(x, y, offset_x, offset_y, scale, menuItems)
self.x,self.y = x,y
self.offset_x,self.offset_y = offset_x,offset_y
self.options = menuItems
self.selected = 1
self.scale = scale
self.keys = {}
self.keys['up'] = false
self.keys['down'] = false
self.keys['enter'] = false
self.ttf = r_ttf
end
2021-02-06 13:37:41 -06:00
function Menu:update ()
end
2021-02-05 20:58:58 -06:00
function Menu:draw ()
for i=1,table.maxn(self.options) do
local this_y = self.y + (self.offset_y * i)
love.graphics.draw(self.options[i][1],
self.x, this_y, 0, self.scale, self.scale)
if (i == self.selected) then
love.graphics.draw(love.graphics.newText(self.ttf, ">>"),
self.x - self.offset_x, this_y, 0, self.scale, self.scale)
end
end
end
function Menu:keypressed(key)
maxn = table.maxn(self.options)
if (key == "return" or key == "space") then
self.keys['enter'] = true
if(self.options[self.selected][2]) then
self.options[self.selected][2]()
end
elseif (key == "up" and self.selected > 1
and self.keys['up'] == false) then
self.keys['up'] = true
self.selected = self.selected - 1
elseif (key == "up" and self.keys['up'] == false) then
self.keys['up'] = true
self.selected = maxn
elseif (key == "down" and self.selected < maxn
and self.keys['down'] == false) then
self.keys['down'] = true
self.selected = self.selected + 1
elseif (key == "down" and self.keys['down'] == false) then
self.keys['down'] = true
self.selected = 1
end
end
function Menu:keyreleased(key)
if (key == "return" or key == "space") then
self.keys['enter'] = false
elseif (key == "up") then
self.keys['up'] = false
elseif (key == "down") then
self.keys['down'] = false
end
end
2021-02-06 00:50:51 -06:00
-- UTIL
--------------------------------------------------------------------------------
2021-02-05 20:58:58 -06:00
function split(inputString, seperator)
local newString = {}
for stringBit in string.gmatch(inputString, "([^"..seperator.."]+)") do
table.insert(newString, stringBit)
end
return newString
end
2021-02-06 00:50:51 -06:00
-- MISC DATA
--------------------------------------------------------------------------------
-- CHARACTERS
------------------------------------------
2021-02-06 13:10:15 -06:00
CHARACTERS = {}
CHARACTERS["jellyfish-lion.png"] = {"Lion Jellyfish", "hey, hey. you know whats shocking?", "rapidpunches", "CC-BY-SA 4.0"}
CHARACTERS["jellyfish-n.png"] = {"Jellyfish N", "(electricity)", "rapidpunches", "CC-BY-SA 4.0"}
-- DEFAULT NAMES
------------------------------------------
NAMES = {"Ignucius", "Penguin", "Tux", "Puffy", "Doktoro", "Espero", "<3", "</3"}
2021-02-06 00:50:51 -06:00
-- LOCAL KEYMAPS
------------------------------------------
2021-02-06 13:10:15 -06:00
KEYMAPS = {}
KEYMAPS[1] = {["accel"] = "w", ["left"] = "a", ["right"] = "d", ["flip"] = "s"}
KEYMAPS[2] = {["accel"] = "up", ["left"] = "left", ["right"] = "right", ["flip"] = "down"}