From 86b62d4801795a8a9a90ca00542d83b9eb6fd49f Mon Sep 17 00:00:00 2001 From: RandomityGuy <31925790+RandomityGuy@users.noreply.github.com> Date: Thu, 13 Jun 2024 00:18:42 +0530 Subject: [PATCH] impl chat finally --- src/Marble.hx | 60 +++++++++---------- src/Settings.hx | 8 +++ src/gui/KeyBindingsGui.hx | 8 +++ src/gui/PlayGui.hx | 117 +++++++++++++++++++++++++++++++++++--- src/net/BitStream.hx | 4 +- src/net/MoveManager.hx | 70 ++++++++++++----------- src/net/NetCommands.hx | 14 +++++ 7 files changed, 209 insertions(+), 72 deletions(-) diff --git a/src/Marble.hx b/src/Marble.hx index 28cc0703..726d8ae7 100644 --- a/src/Marble.hx +++ b/src/Marble.hx @@ -2111,37 +2111,39 @@ class Marble extends GameObject { move.d = new Vector(); move.d.x = Gamepad.getAxis(Settings.gamepadSettings.moveYAxis); move.d.y = -Gamepad.getAxis(Settings.gamepadSettings.moveXAxis); - if (Key.isDown(Settings.controlsSettings.forward)) { - move.d.x -= 1; - } - if (Key.isDown(Settings.controlsSettings.backward)) { - move.d.x += 1; - } - if (Key.isDown(Settings.controlsSettings.left)) { - move.d.y += 1; - } - if (Key.isDown(Settings.controlsSettings.right)) { - move.d.y -= 1; - } - if (Key.isDown(Settings.controlsSettings.jump) - || MarbleGame.instance.touchInput.jumpButton.pressed - || Gamepad.isDown(Settings.gamepadSettings.jump)) { - move.jump = true; - } - if ((!Util.isTouchDevice() && Key.isDown(Settings.controlsSettings.powerup)) - || (Util.isTouchDevice() && MarbleGame.instance.touchInput.powerupButton.pressed) - || Gamepad.isDown(Settings.gamepadSettings.powerup)) { - move.powerup = true; - } + if (@:privateAccess !MarbleGame.instance.world.playGui.isChatFocused()) { + if (Key.isDown(Settings.controlsSettings.forward)) { + move.d.x -= 1; + } + if (Key.isDown(Settings.controlsSettings.backward)) { + move.d.x += 1; + } + if (Key.isDown(Settings.controlsSettings.left)) { + move.d.y += 1; + } + if (Key.isDown(Settings.controlsSettings.right)) { + move.d.y -= 1; + } + if (Key.isDown(Settings.controlsSettings.jump) + || MarbleGame.instance.touchInput.jumpButton.pressed + || Gamepad.isDown(Settings.gamepadSettings.jump)) { + move.jump = true; + } + if ((!Util.isTouchDevice() && Key.isDown(Settings.controlsSettings.powerup)) + || (Util.isTouchDevice() && MarbleGame.instance.touchInput.powerupButton.pressed) + || Gamepad.isDown(Settings.gamepadSettings.powerup)) { + move.powerup = true; + } - if (Key.isDown(Settings.controlsSettings.blast) - || (MarbleGame.instance.touchInput.blastbutton.pressed) - || Gamepad.isDown(Settings.gamepadSettings.blast)) - move.blast = true; + if (Key.isDown(Settings.controlsSettings.blast) + || (MarbleGame.instance.touchInput.blastbutton.pressed) + || Gamepad.isDown(Settings.gamepadSettings.blast)) + move.blast = true; - if (MarbleGame.instance.touchInput.movementInput.pressed) { - move.d.y = -MarbleGame.instance.touchInput.movementInput.value.x; - move.d.x = MarbleGame.instance.touchInput.movementInput.value.y; + if (MarbleGame.instance.touchInput.movementInput.pressed) { + move.d.y = -MarbleGame.instance.touchInput.movementInput.value.x; + move.d.x = MarbleGame.instance.touchInput.movementInput.value.y; + } } return move; } diff --git a/src/Settings.hx b/src/Settings.hx index 2294a177..d484a367 100644 --- a/src/Settings.hx +++ b/src/Settings.hx @@ -70,6 +70,7 @@ typedef ControlsSettings = { var respawn:Int; var blast:Int; var rewind:Int; + var chat:Int; } typedef TouchSettings = { @@ -161,6 +162,7 @@ class Settings { respawn: Key.BACKSPACE, blast: Key.MOUSE_RIGHT, rewind: Key.R, + chat: Key.T }; public static var touchSettings:TouchSettings = { @@ -387,6 +389,9 @@ class Settings { if (controlsSettings.rewind == 0) { controlsSettings.rewind = Key.R; } + if (controlsSettings.chat == 0) { + controlsSettings.chat = Key.T; + } if (touchSettings.blastButtonPos == null) { touchSettings.blastButtonPos = [300, 240]; touchSettings.blastButtonSize = 60; @@ -450,6 +455,9 @@ class Settings { if (controlsSettings.rewind == null) { controlsSettings.rewind = Key.R; } + if (controlsSettings.chat == null) { + controlsSettings.chat = Key.T; + } if (optionsSettings.rewindEnabled == null) { optionsSettings.rewindEnabled = false; } diff --git a/src/gui/KeyBindingsGui.hx b/src/gui/KeyBindingsGui.hx index 41411473..273b88f1 100644 --- a/src/gui/KeyBindingsGui.hx +++ b/src/gui/KeyBindingsGui.hx @@ -56,6 +56,8 @@ class KeyBindingsGui extends GuiImage { return "Free Look"; if (Settings.controlsSettings.rewind == key && bindingName != "Rewind") return "Rewind"; + if (Settings.controlsSettings.chat == key && bindingName != "Chat") + return "Chat"; return null; } @@ -179,6 +181,10 @@ class KeyBindingsGui extends GuiImage { b12.pressedAction = (e) -> { remapFunc("Rewind", (key) -> Settings.controlsSettings.rewind = key, b12); } + var b13 = btnListLeft.addButton(0, 'Chat: ${Util.getKeyForButton2(Settings.controlsSettings.chat)}', (e) -> {}); + b13.pressedAction = (e) -> { + remapFunc("Chat", (key) -> Settings.controlsSettings.chat = key, b13); + } var bottomBar = new GuiControl(); bottomBar.position = new Vector(0, 590); @@ -226,6 +232,8 @@ class KeyBindingsGui extends GuiImage { } if (prevSelected == 1 && selectedColumn == 0) { btnListRight.selected = btnListLeft.selected; + if (btnListRight.selected > btnListRight.buttons.length - 1) + btnListRight.selected = btnListRight.buttons.length - 1; } if (_prevMousePosition == null || !_prevMousePosition.equals(mouseState.position)) { for (i in 0...btnListLeft.buttons.length) { diff --git a/src/gui/PlayGui.hx b/src/gui/PlayGui.hx index 6a70430d..5a17d33b 100644 --- a/src/gui/PlayGui.hx +++ b/src/gui/PlayGui.hx @@ -1,5 +1,7 @@ package gui; +import net.NetCommands; +import hxd.Key; import net.NetPacket.ScoreboardPacket; import net.Net; import h3d.Matrix; @@ -46,13 +48,20 @@ class MiddleMessage { @:publicFields @:structInit -typedef PlayerInfo = { +class PlayerInfo { var id:Int; var name:String; var us:Bool; var score:Int; } +@:publicFields +@:structInit +class ChatMessage { + var text:String; + var sendTime:Float; +} + class PlayGui { var scene2d:h2d.Scene; @@ -108,7 +117,10 @@ class PlayGui { var chatHudCtrl:GuiControl; var chatHud:GuiMLText; - var chats:Array; + var chatHudBg:GuiMLText; + var chatHudInput:GuiTextInput; + var chats:Array; + var chatFocused:Bool = false; public function dispose() { if (_init) { @@ -664,21 +676,101 @@ class PlayGui { var arial14fontdata = ResourceLoader.getFileEntry("data/font/Arial Bold.fnt"); var arial14b = new BitmapFont(arial14fontdata.entry); @:privateAccess arial14b.loader = ResourceLoader.loader; - var arial14 = arial14b.toSdfFont(cast 16 * Settings.uiScale, MultiChannel); + var arial14 = arial14b.toSdfFont(cast 15 * Settings.uiScale, MultiChannel); this.chatHudCtrl = new GuiControl(); this.chatHudCtrl.position = new Vector(playGuiCtrl.extent.x - 201, 150); - this.chatHudCtrl.extent = new Vector(150, 200); + this.chatHudCtrl.extent = new Vector(200, 250); this.chatHudCtrl.horizSizing = Left; this.playGuiCtrl.addChild(chatHudCtrl); this.chats = []; + + this.chatHudBg = new GuiMLText(arial14, (s) -> arial14); + this.chatHudBg.position = new Vector(1, 21); + this.chatHudBg.extent = new Vector(200, 250); + this.chatHudBg.text.textColor = 0; + this.chatHudCtrl.addChild(chatHudBg); + this.chatHud = new GuiMLText(arial14, (s) -> arial14); - this.chatHud.position = new Vector(0, 0); - this.chatHud.extent = new Vector(150, 200); + this.chatHud.position = new Vector(0, 20); + this.chatHud.extent = new Vector(200, 250); this.chatHudCtrl.addChild(chatHud); - this.chatHud.text.text = "User 1: Hi
User 2: Hello
User 3: Hey"; + this.chatHudInput = new GuiTextInput(arial14); + this.chatHudInput.position = new Vector(0, 0); + this.chatHudInput.extent = new Vector(200, 20); + @:privateAccess this.chatHudInput.text.interactive.forceAnywherefocus = true; + this.chatHudCtrl.addChild(chatHudInput); + + var sendText = ""; + + this.chatHudInput.text.onFocus = (e) -> { + chatFocused = true; + } + + this.chatHudInput.text.onFocusLost = (e) -> { + sendText = ""; + chatFocused = false; + } + + this.chatHudInput.text.onKeyDown = (e) -> { + if (e.keyCode == Key.ENTER) { + sendText = '${StringTools.htmlEscape(Settings.highscoreName.substr(0, 20))}: ${StringTools.htmlEscape(this.chatHudInput.text.text.substr(0, 50))}'; + if (Net.isClient) { + NetCommands.sendChatMessage(StringTools.htmlEscape(sendText)); + } + if (Net.isHost) { + NetCommands.sendServerChatMessage(StringTools.htmlEscape(sendText)); + } + this.chatHudInput.text.text = ""; + chatFocused = false; + } + if (e.keyCode == Key.ESCAPE) { + this.chatHudInput.text.text = ""; + chatFocused = false; + @:privateAccess Key.keyPressed[Key.ESCAPE] = 0; // consume escape + } + } + + this.chatHud.text.text = ""; + } + + public inline function isChatFocused() { + return chatFocused; + } + + public function addChatMessage(text:String) { + var realText = StringTools.htmlUnescape(text); + this.chats.push({ + text: realText, + sendTime: MarbleGame.instance.world.timeState.timeSinceLoad + }); + if (this.chats.length > 10) { + this.chats = this.chats.slice(this.chats.length - 10); + } + redrawChatMessages(); + } + + function redrawChatMessages() { + var joined = this.chats.map(x -> x.text).join("
"); + this.chatHud.text.text = joined; + this.chatHudBg.text.text = StringTools.replace(joined, '#F29515', '#000000'); + } + + function tickChats(timeState:TimeState) { + var needsRedraw = false; + while (this.chats.length > 0) { + if (this.chats[0].sendTime + 10 < timeState.timeSinceLoad) { + this.chats.shift(); + needsRedraw = true; + } else { + break; + } + } + if (needsRedraw) { + redrawChatMessages(); + } } var blastValue:Float = 0; @@ -1141,6 +1233,17 @@ class PlayGui { this.fpsMeter.text.text = '${Math.floor(ProfilerUI.instance.fps)} fps'; } this.updateMiddleMessages(timeState.dt); + if (Net.isMP) { + if (!chatFocused) { + if (Key.isPressed(Settings.controlsSettings.chat)) { + if (this.chatHudCtrl != null) { + this.chatHudInput.text.focus(); + } + } + } + + tickChats(timeState); + } } function updateMiddleMessages(dt:Float) { diff --git a/src/net/BitStream.hx b/src/net/BitStream.hx index 63f6849f..7210db53 100644 --- a/src/net/BitStream.hx +++ b/src/net/BitStream.hx @@ -69,7 +69,7 @@ class InputBitStream { } public function readString() { - var length = readByte(); + var length = readUInt16(); var str = ""; for (i in 0...length) { str += String.fromCharCode(readByte()); @@ -146,7 +146,7 @@ class OutputBitStream { } public function writeString(value:String) { - writeByte(value.length); + writeUInt16(value.length); for (i in 0...value.length) { writeByte(StringTools.fastCodeAt(value, i)); } diff --git a/src/net/MoveManager.hx b/src/net/MoveManager.hx index be0e7817..0ec06c2c 100644 --- a/src/net/MoveManager.hx +++ b/src/net/MoveManager.hx @@ -76,37 +76,39 @@ class MoveManager { if (!MarbleGame.instance.paused) { move.d.x = Gamepad.getAxis(Settings.gamepadSettings.moveYAxis); move.d.y = -Gamepad.getAxis(Settings.gamepadSettings.moveXAxis); - if (Key.isDown(Settings.controlsSettings.forward)) { - move.d.x -= 1; - } - if (Key.isDown(Settings.controlsSettings.backward)) { - move.d.x += 1; - } - if (Key.isDown(Settings.controlsSettings.left)) { - move.d.y += 1; - } - if (Key.isDown(Settings.controlsSettings.right)) { - move.d.y -= 1; - } - if (Key.isDown(Settings.controlsSettings.jump) - || MarbleGame.instance.touchInput.jumpButton.pressed - || Gamepad.isDown(Settings.gamepadSettings.jump)) { - move.jump = true; - } - if ((!Util.isTouchDevice() && Key.isDown(Settings.controlsSettings.powerup)) - || (Util.isTouchDevice() && MarbleGame.instance.touchInput.powerupButton.pressed) - || Gamepad.isDown(Settings.gamepadSettings.powerup)) { - move.powerup = true; - } + if (@:privateAccess !MarbleGame.instance.world.playGui.isChatFocused()) { + if (Key.isDown(Settings.controlsSettings.forward)) { + move.d.x -= 1; + } + if (Key.isDown(Settings.controlsSettings.backward)) { + move.d.x += 1; + } + if (Key.isDown(Settings.controlsSettings.left)) { + move.d.y += 1; + } + if (Key.isDown(Settings.controlsSettings.right)) { + move.d.y -= 1; + } + if (Key.isDown(Settings.controlsSettings.jump) + || MarbleGame.instance.touchInput.jumpButton.pressed + || Gamepad.isDown(Settings.gamepadSettings.jump)) { + move.jump = true; + } + if ((!Util.isTouchDevice() && Key.isDown(Settings.controlsSettings.powerup)) + || (Util.isTouchDevice() && MarbleGame.instance.touchInput.powerupButton.pressed) + || Gamepad.isDown(Settings.gamepadSettings.powerup)) { + move.powerup = true; + } - if (Key.isDown(Settings.controlsSettings.blast) - || (MarbleGame.instance.touchInput.blastbutton.pressed) - || Gamepad.isDown(Settings.gamepadSettings.blast)) - move.blast = true; + if (Key.isDown(Settings.controlsSettings.blast) + || (MarbleGame.instance.touchInput.blastbutton.pressed) + || Gamepad.isDown(Settings.gamepadSettings.blast)) + move.blast = true; - if (MarbleGame.instance.touchInput.movementInput.pressed) { - move.d.y = -MarbleGame.instance.touchInput.movementInput.value.x; - move.d.x = MarbleGame.instance.touchInput.movementInput.value.y; + if (MarbleGame.instance.touchInput.movementInput.pressed) { + move.d.y = -MarbleGame.instance.touchInput.movementInput.value.x; + move.d.x = MarbleGame.instance.touchInput.movementInput.value.y; + } } // quantize moves for client @@ -214,12 +216,12 @@ class MoveManager { if (queuedMoves.length > serverMaxMoveListSize || (serverAvgMoveListSize > serverTargetMoveListSize + serverMoveListSizeSlack && queuedMoves.length > serverTargetMoveListSize)) { - if (queuedMoves.length > serverMaxMoveListSize) { - var dropAmt = queuedMoves.length - serverTargetMoveListSize; - while (dropAmt-- > 0) { - queuedMoves.pop(); - } + // if (queuedMoves.length > serverMaxMoveListSize) { + var dropAmt = queuedMoves.length - serverTargetMoveListSize; + while (dropAmt-- > 0) { + queuedMoves.pop(); } + // } serverAvgMoveListSize = serverTargetMoveListSize; // serverAbnormalMoveCount++; // if (serverAbnormalMoveCount > 3) { diff --git a/src/net/NetCommands.hx b/src/net/NetCommands.hx index 8b66631d..0f304b13 100644 --- a/src/net/NetCommands.hx +++ b/src/net/NetCommands.hx @@ -326,4 +326,18 @@ class NetCommands { ping(Console.time()); } } + + @:rpc(client) public static function sendChatMessage(msg:String) { + if (Net.isHost) { + sendServerChatMessage(msg); + } + } + + @:rpc(server) public static function sendServerChatMessage(msg:String) { + if (MarbleGame.instance.world != null) { + if (MarbleGame.instance.world._ready) { + @:privateAccess MarbleGame.instance.world.playGui.addChatMessage(msg); + } + } + } }