diff options
| author | metamuffin <metamuffin@disroot.org> | 2024-06-25 23:34:10 +0200 | 
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2024-06-25 23:34:10 +0200 | 
| commit | 4dc15a1e86ef1ae985fdf36f1a84d07b1de99ea7 (patch) | |
| tree | 6a262cd2be9abee480adda3e367c7f8abf8845d6 /test-client | |
| parent | 84c90e84a1e0d6cd2eae36fd8888354b4e23c354 (diff) | |
| download | hurrycurry-4dc15a1e86ef1ae985fdf36f1a84d07b1de99ea7.tar hurrycurry-4dc15a1e86ef1ae985fdf36f1a84d07b1de99ea7.tar.bz2 hurrycurry-4dc15a1e86ef1ae985fdf36f1a84d07b1de99ea7.tar.zst | |
server can change map at runtime
Diffstat (limited to 'test-client')
| -rw-r--r-- | test-client/index.html | 6 | ||||
| -rw-r--r-- | test-client/main.ts | 32 | ||||
| -rw-r--r-- | test-client/protocol.ts | 6 | 
3 files changed, 40 insertions, 4 deletions
| diff --git a/test-client/index.html b/test-client/index.html index d1cd7133..63d02f9e 100644 --- a/test-client/index.html +++ b/test-client/index.html @@ -31,6 +31,12 @@              noscript {                  color: white;              } +            input[type="text"] { +                position: absolute; +                top: 0px; +                left: 0px; +                font-size: 30px; +            }          </style>      </head>      <body> diff --git a/test-client/main.ts b/test-client/main.ts index 69a4ff69..0f841b06 100644 --- a/test-client/main.ts +++ b/test-client/main.ts @@ -92,7 +92,6 @@ export const items_removed = new Set<ItemData>()  export let data: Gamedata = { item_names: [], tile_names: [], spawn: [0, 0], tile_collide: [], tile_interact: [] } -  export let my_id: PlayerID = -1  export const camera: V2 = { x: 0, y: 0 }  export const interact_target_anim: V2 = { x: 0, y: 0 } @@ -107,6 +106,8 @@ function packet(p: PacketC) {      switch (p.type) {          case "init":              my_id = p.id +            break; +        case "data":              data = p.data              break;          case "add_player": { @@ -172,7 +173,8 @@ function packet(p: PacketC) {              break;          }          case "update_map": -            tiles.set(p.pos.toString(), { x: p.pos[0], y: p.pos[1], kind: p.tile }) +            if (p.kind !== undefined && p.kind !== null) tiles.set(p.tile.toString(), { x: p.tile[0], y: p.tile[1], kind: p.kind }) +            else tiles.delete(p.tile.toString())              break;          case "communicate": {              const player = players.get(p.player)! @@ -188,9 +190,14 @@ function packet(p: PacketC) {      }  } +export let chat: null | HTMLInputElement = null; +  export const keys_down = new Set();  const HANDLED_KEYS = ["KeyW", "KeyA", "KeyS", "KeyD", "Space"]  function keyboard(ev: KeyboardEvent, down: boolean) { +    if (down && ev.code == "Enter") return toggle_chat() +    else if (down && ev.code == "Escape" && chat) return close_chat() +    else if (chat) return      if (HANDLED_KEYS.includes(ev.code)) ev.preventDefault()      if (!keys_down.has("Space") && ev.code == "Space" && down) set_interact(true)      if (keys_down.has("Space") && ev.code == "Space" && !down) set_interact(false) @@ -198,6 +205,27 @@ function keyboard(ev: KeyboardEvent, down: boolean) {      else keys_down.delete(ev.code)  } +function close_chat() { +    if (!chat) return +    chat.remove() +    canvas.focus() +    chat = null; +} +function toggle_chat() { +    if (chat) { +        if (chat.value.length) send({ type: "communicate", message: { text: chat.value } }) +        chat.remove() +        canvas.focus() +        chat = null; +    } else { +        chat = document.createElement("input") +        chat.type = "text" +        chat.placeholder = "Message" +        document.body.append(chat) +        chat.focus() +    } +} +  export function get_interact_target(): V2 | undefined {      if (interacting) return interacting      const me = players.get(my_id) diff --git a/test-client/protocol.ts b/test-client/protocol.ts index 48a1cca7..f4ffba01 100644 --- a/test-client/protocol.ts +++ b/test-client/protocol.ts @@ -32,10 +32,12 @@ export type PacketS =      { type: "join", name: string, character: number } // You join, sent as first packet.      | { type: "position", pos: Vec2, rot: number } // Update your position and rotation in radians (0 is -y)      | { type: "interact", pos: Vec2, edge: boolean } // Interact with some tile. edge is true when pressing and false when releasing interact button +    | { type: "communicate", message: Message } // Send a message      | { type: "collide", player: PlayerID, force: Vec2 } // Apply force to another player as a result of a collision  export type PacketC = -    { type: "init", id: PlayerID, data: Gamedata } // You joined +    { type: "init", id: PlayerID } // You just connected. This is your id for this session. +    | { type: "data", data: Gamedata } // Game data was changed      | { type: "add_player", id: PlayerID, name: string, position: Vec2, character: number } // Somebody else joined (or was already in the game)      | { type: "remove_player", id: PlayerID }  // Somebody left      | { type: "position", player: PlayerID, pos: Vec2, rot: number } // Update the position of a players (your own position is included here) @@ -44,7 +46,7 @@ export type PacketC =      | { type: "set_tile_item", tile: Vec2, item?: ItemIndex } // A tile changed its item      | { type: "set_player_item", player: PlayerID, item?: ItemIndex } // A player changed their item      | { type: "set_active", tile: Vec2, progress?: number, warn: boolean } // A tile is doing something. progress goes from 0 to 1, then null when finished -    | { type: "update_map", pos: Vec2, tile: TileIndex, neighbors: [TileIndex | null] } // A map tile was changed +    | { type: "update_map", tile: Vec2, kind: TileIndex | null, neighbors: [TileIndex | null] } // A map tile was changed      | { type: "communicate", player: PlayerID, message?: Message } // A player wants to communicate something, message is null when cleared      | { type: "error", message?: Message } // Your client did something wrong. | 
