diff options
-rw-r--r-- | client/scripts/map.gd | 57 | ||||
-rw-r--r-- | client/scripts/map/chair.gd | 13 | ||||
-rw-r--r-- | client/scripts/map/counter.gd | 61 | ||||
-rw-r--r-- | client/scripts/map/counter_base.gd | 13 | ||||
-rw-r--r-- | client/scripts/map/cutting_board.gd | 1 | ||||
-rw-r--r-- | client/scripts/map/floor.gd | 24 | ||||
-rw-r--r-- | client/scripts/map/full_tile.gd | 13 | ||||
-rw-r--r-- | client/scripts/map/table.gd | 6 | ||||
-rw-r--r-- | client/scripts/map/trash.gd | 6 | ||||
-rw-r--r-- | client/scripts/map/wall.gd | 6 | ||||
-rw-r--r-- | client/scripts/map/wall_tile.gd | 10 | ||||
-rw-r--r-- | client/scripts/multiplayer.gd | 116 |
12 files changed, 326 insertions, 0 deletions
diff --git a/client/scripts/map.gd b/client/scripts/map.gd new file mode 100644 index 00000000..64d15723 --- /dev/null +++ b/client/scripts/map.gd @@ -0,0 +1,57 @@ +extends Node3D + +func _ready(): + Multiplayer.connect("update_map", update) + +func update(pos, tile_name, neighbors): + var instance: Floor + match tile_name: + "trash": + instance = Trash.new() + "tomato-crate": + instance = CounterBase.new() + "cuttingboard": + instance = CounterBase.new() + "counter": + instance = CounterBase.new() + "flour-crate": + instance = CounterBase.new() + "oven": + instance = CounterBase.new() + "raw-steak-crate": + instance = CounterBase.new() + "pan": + instance = CounterBase.new() + "watercooler": + instance = CounterBase.new() + "sink": + instance = CounterBase.new() + "dirty-plate-crate": + instance = CounterBase.new() + "wall": + instance = Wall.new() + "chair": + instance = Chair.new() + "table": + instance = Table.new() + "floor": + instance = Floor.new() + "window": + instance = Wall.new() + "door": + instance = Wall.new() + _: + instance = Floor.new() + + var node_name = str(pos) + + if has_node(node_name): + queue_free_rename(get_node(node_name)) + + instance.setup(node_name, neighbors) + instance.position = Vector3(pos[0], 0, pos[1]) + add_child(instance) + +func queue_free_rename(node: Node) -> void: + node.name += "_queued_free" + node.queue_free() diff --git a/client/scripts/map/chair.gd b/client/scripts/map/chair.gd new file mode 100644 index 00000000..e51d7864 --- /dev/null +++ b/client/scripts/map/chair.gd @@ -0,0 +1,13 @@ +class_name Chair +extends Floor + +func setup(rename: String, neighbors: Array): + super.setup(rename, neighbors) + var chair = load("res://models/prefabs/map/chair_A.tscn").instantiate() + var facing = 0; + for i in range(4): + if tile_name(neighbors[i]) == "table": + facing = i + break + base.add_child(chair) + turn_facing(facing) diff --git a/client/scripts/map/counter.gd b/client/scripts/map/counter.gd new file mode 100644 index 00000000..686369cf --- /dev/null +++ b/client/scripts/map/counter.gd @@ -0,0 +1,61 @@ +class_name Counter +extends FullTile + +var counters = [ + "counter", + "pan", + "sink", + "oven", +] + +enum CounterKind { + OUTER_CORNER, + STRAIGHT, + STRAIGHT_BACKSPLASH +} + +var facing: int = 0 +var kind: CounterKind = CounterKind.STRAIGHT + +func setup(rename: String, neighbors: Array): + super.setup(rename, neighbors) + var edges = neighbors.duplicate() + for i in range(4): + var i_name = tile_name(edges[i]) + if is_counter(i_name): + edges[i] = "counter" + else: + edges[i] = tile_name(edges[i]) + + var series: int = 0 + var last_series: int = 0 + var adj: Array = [] + + for i in range(4): + if edges[i] == "floor": + last_series += 1 + adj.append(i) + if last_series > series: + series = last_series + else: + last_series = 0 + + var count = 4 - adj.size() + + # we can neither find out whether it is an inner corner nor an outer corner + # backsplash + if series == 1&&count == 3: + facing = adj[0] % 4 + + if edges[(adj[0] + 2) % 4] == "wall": + kind = CounterKind.STRAIGHT_BACKSPLASH + else: + kind = CounterKind.STRAIGHT + elif series == 2&&count == 2: + facing = (adj[0] + 1) % 4 + kind = CounterKind.OUTER_CORNER + +func is_counter(tile_name_t) -> bool: + if tile_name_t == null: + return false + return name.ends_with("crate")||counters.has(name) diff --git a/client/scripts/map/counter_base.gd b/client/scripts/map/counter_base.gd new file mode 100644 index 00000000..32f5cc06 --- /dev/null +++ b/client/scripts/map/counter_base.gd @@ -0,0 +1,13 @@ +extends Counter +class_name CounterBase + +func setup(rename: String, neighbors: Array): + super.setup(rename, neighbors) + match kind: + CounterKind.OUTER_CORNER: + base.add_child(load("res://models/prefabs/map/kitchencounter_outercorner.tscn").instantiate()) + CounterKind.STRAIGHT: + base.add_child(load("res://models/prefabs/map/kitchencounter_straight_A.tscn").instantiate()) + CounterKind.STRAIGHT_BACKSPLASH: + base.add_child(load("res://models/prefabs/map/kitchencounter_straight_A_backsplash.tscn").instantiate()) + turn_facing(facing) diff --git a/client/scripts/map/cutting_board.gd b/client/scripts/map/cutting_board.gd new file mode 100644 index 00000000..4be394d0 --- /dev/null +++ b/client/scripts/map/cutting_board.gd @@ -0,0 +1 @@ +extends CounterBase diff --git a/client/scripts/map/floor.gd b/client/scripts/map/floor.gd new file mode 100644 index 00000000..d4e61c2d --- /dev/null +++ b/client/scripts/map/floor.gd @@ -0,0 +1,24 @@ +class_name Floor +extends Node3D + +var base = Node3D.new() + +enum Facing { + NEG_Y = 0, + NEG_X = 1, + Y = 2, + X = 3, +} + +func setup(rename: String, _neighbors: Array): + add_child(load("res://models/prefabs/map/floor_kitchen_small.tscn").instantiate()) + add_child(base) + self.name = rename + +func turn_facing(facing: Facing): + base.rotate_y(facing * 0.5 * PI + PI) + +func tile_name(idx): + if idx == null: + return null + return Multiplayer.tile_names[idx] diff --git a/client/scripts/map/full_tile.gd b/client/scripts/map/full_tile.gd new file mode 100644 index 00000000..c05e0457 --- /dev/null +++ b/client/scripts/map/full_tile.gd @@ -0,0 +1,13 @@ +class_name FullTile +extends Floor + +var static_body = StaticBody3D.new() + +func setup(rename: String, neighbors: Array): + super.setup(rename, neighbors) + var shape = CollisionShape3D.new() + var box = BoxShape3D.new() + shape.position.y += .5 + shape.shape = box + static_body.add_child(shape) + base.add_child(static_body) diff --git a/client/scripts/map/table.gd b/client/scripts/map/table.gd new file mode 100644 index 00000000..937ef097 --- /dev/null +++ b/client/scripts/map/table.gd @@ -0,0 +1,6 @@ +class_name Table +extends FullTile + +func setup(rename: String, neighbors: Array): + super.setup(rename, neighbors) + base.add_child(load("res://models/prefabs/map/table_round_A_small.tscn").instantiate()) diff --git a/client/scripts/map/trash.gd b/client/scripts/map/trash.gd new file mode 100644 index 00000000..1ec05e49 --- /dev/null +++ b/client/scripts/map/trash.gd @@ -0,0 +1,6 @@ +class_name Trash +extends FullTile + +func setup(rename: String, neighbors: Array): + super.setup(rename, neighbors) + base.add_child(load("res://models/prefabs/map/crate_trash.tscn").instantiate()) diff --git a/client/scripts/map/wall.gd b/client/scripts/map/wall.gd new file mode 100644 index 00000000..dd6d3495 --- /dev/null +++ b/client/scripts/map/wall.gd @@ -0,0 +1,6 @@ +class_name Wall +extends WallTile + +func setup(rename: String, neighbors: Array): + super.setup(rename, neighbors) + base.add_child(load("res://models/prefabs/map/wall.tscn").instantiate()) diff --git a/client/scripts/map/wall_tile.gd b/client/scripts/map/wall_tile.gd new file mode 100644 index 00000000..d107d73e --- /dev/null +++ b/client/scripts/map/wall_tile.gd @@ -0,0 +1,10 @@ +class_name WallTile +extends FullTile + +func setup(rename: String, neighbors: Array): + super.setup(rename, neighbors) + var facing = 0 + for i in range(4): + if neighbors[i] != null&&tile_name(neighbors[i]) != "wall": + facing = i + turn_facing(facing) diff --git a/client/scripts/multiplayer.gd b/client/scripts/multiplayer.gd new file mode 100644 index 00000000..bd488900 --- /dev/null +++ b/client/scripts/multiplayer.gd @@ -0,0 +1,116 @@ +extends Node + +signal update_map + +var connected := false +var socket := WebSocketPeer.new() + +var item_names = [] +var tile_names = [] +var player_id = -1 + +var other_players = {} + +func connectClient(url: String): + socket.connect_to_url(url) + connected = true + +func _process(_delta): + if connected: + socket.poll() + var state = socket.get_ready_state() + if state == WebSocketPeer.STATE_OPEN: + while socket.get_available_packet_count(): + handle_packet(socket.get_packet()) + elif state == WebSocketPeer.STATE_CONNECTING: + print("connecting") + elif state == WebSocketPeer.STATE_CLOSING: + # Keep polling to achieve proper close. + print("closing") + pass + elif state == WebSocketPeer.STATE_CLOSED: + var code = socket.get_close_code() + var reason = socket.get_close_reason() + print("WebSocket closed with code: %d, reason %s. Clean: %s" % [code, reason, code != -1]) + +func handle_packet(bytes: PackedByteArray): + var decoded = decode_packet(bytes) + + if decoded == null: + return + + var packet_type: String = decoded["type"] + match packet_type: + "init": + player_id = decoded["id"] + item_names = decoded["data"]["item_names"] + tile_names = decoded["data"]["tile_names"] + "add_player": + var id = decoded["id"] + var player_name = decoded["name"] + #var item = decoded["item"] + var pos = decoded["position"] + var char = decoded["character"] + other_players[id] = [player_name, char] + "remove_player": + var id = decoded["id"] + other_players.erase(id) + "position": + var id = decoded["player"] + var pos = decoded["pos"] + var rot = decoded["rot"] + "take_item": + var tile = decoded["tile"] + var player_id = decoded["player"] + "put_item": + var tile = decoded["tile"] + var player_id = decoded["player"] + "produce_item": + var tile = decoded["tile"] + var item = decoded["item"] + "consume_item": + var tile = decoded["tile"] + "set_active": + var tile = decoded["tile"] + var progress = decoded["progress"] + "update_map": + var tile = decoded["tile"] + var pos = decoded["pos"] + var neighbors = decoded["neighbors"] + emit_signal("update_map", pos, tile_names[tile], neighbors) + _: + push_error("Unrecognized packet type: %s" % packet_type) + +func send_join(player_name: String): + send_packet({ + "type": "join", + "name": player_name + }) + +func send_position(pos: Vector2, rotation: float): + send_packet({ + "type": "position", + "pos": [pos.x, pos.y], + "rot": rotation + }) + +func send_interact(pos: Vector2, edge: bool): + send_packet({ + "type": "interact", + "pos": [pos.x, pos.y], + "edge": edge + }) + +func send_packet(packet): + var json = JSON.stringify(packet) + socket.send_text(json) + +func decode_packet(bytes: PackedByteArray): + var json = JSON.new() + var in_str = bytes.get_string_from_utf8() + var error = json.parse(in_str) + if error == OK: + return json.data + else: + print("Decode of packet failed: %s in %s" % [json.get_error_message(), in_str]) + return null |