Last active
May 8, 2021 14:51
-
-
Save wolf81/e3943a103faf3232b7f66319432eb37f to your computer and use it in GitHub Desktop.
local client / server for singleplayer; use only client when joining a game, use server with 2 clients for hosting a game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local connect = Class { __includes = Scene } | |
| function connect:init() | |
| Scene.init(self, "CONNECT", textures['background-menu']) | |
| end | |
| function connect:enter(previous, gameMode, host, port) | |
| self.gameMode = gameMode | |
| self.clients = {} | |
| host = host or NET_DEFAULT_HOST | |
| port = port or NET_DEFAULT_PORT | |
| local isHost = self.gameMode == GAME_MODE_SINGLE or self.gameMode == GAME_MODE_SERVER | |
| if isHost then | |
| self.server = Server(NET_DEFAULT_PORT) | |
| self.server:start() | |
| end | |
| local playerControl = Control(CONTROL_MODE_PLAYER) | |
| local cpuControl = Control(CONTROL_MODE_CPU) | |
| if self.gameMode == GAME_MODE_SINGLE then | |
| self.clients[#self.clients + 1] = Client(host, port, playerControl) -- player: red | |
| self.clients[#self.clients + 1] = Client(host, port, cpuControl) -- cpu: blue | |
| elseif self.gameMode == GAME_MODE_SERVER then | |
| self.clients[#self.clients + 1] = Client(host, port, playerControl) -- player: red | |
| self:setTitle("HOST GAME") | |
| elseif self.gameMode == GAME_MODE_CLIENT then | |
| self.clients[#self.clients + 1] = Client(host, port, playerControl) -- player: blue | |
| end | |
| for _, client in ipairs(self.clients) do client:connect() end | |
| self.clients[1].started = function() | |
| -- preload animations | |
| animations:preload() | |
| return Gamestate.switch(Game { }, self.server, self.clients) | |
| end | |
| end | |
| function connect:update(dt) | |
| Scene.update(self, dt) | |
| local x = (VIRTUAL_WIDTH - UI_CONTROL_WIDTH) / 2 | |
| local y = (VIRTUAL_HEIGHT - UI_CONTROL_HEIGHT) / 2 | |
| suit.layout:reset(x, y, UI_PADDING, UI_PADDING) | |
| if self.gameMode == GAME_MODE_SINGLE then | |
| suit.Label("Starting game ... ", suit.layout:row(UI_CONTROL_WIDTH, UI_CONTROL_HEIGHT)) | |
| elseif self.gameMode == GAME_MODE_SERVER then | |
| suit.Label("Waiting for client ...", suit.layout:row(UI_CONTROL_WIDTH, UI_CONTROL_HEIGHT)) | |
| elseif self.gameMode == GAME_MODE_CLIENT then | |
| suit.Label("Connecting with server ... ", suit.layout:row(UI_CONTROL_WIDTH, UI_CONTROL_HEIGHT)) | |
| end | |
| if self.server ~= nil then self.server:update(dt) end | |
| for _, client in pairs(self.clients) do client:update(dt) end | |
| end | |
| function connect:draw() | |
| Scene.draw(self) | |
| suit.draw() | |
| end | |
| function connect:keyreleased(key, code) | |
| if key == 'escape' then | |
| self.clients[1]:disconnect() | |
| if self.server ~= nil then self.server:stop() end | |
| Gamestate.switch(Menu {}) | |
| end | |
| end | |
| return connect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment