|
| 1 | +extends Node2D |
| 2 | + |
| 3 | +var width |
| 4 | +var height |
| 5 | +var tilemap |
| 6 | +var server |
| 7 | + |
| 8 | +const PORT = 10101 |
| 9 | + |
| 10 | +const YELLOW = 49 # ascii 1 |
| 11 | + |
| 12 | +# Called when the node enters the scene tree for the first time. |
| 13 | +func _ready(): |
| 14 | + tilemap = get_node("TileMap") |
| 15 | + update_width_height() |
| 16 | + clear_display() |
| 17 | + |
| 18 | + print("listening on port ", PORT) |
| 19 | + server = TCP_Server.new() |
| 20 | + server.listen(PORT) |
| 21 | + |
| 22 | +func clear_display(): |
| 23 | + for x in range(width): |
| 24 | + for y in range(height): |
| 25 | + tilemap.set_cell(x, y, 0) |
| 26 | + |
| 27 | +# Called every frame. 'delta' is the elapsed time since the previous frame. |
| 28 | +func _process(delta): |
| 29 | + if server.is_connection_available(): |
| 30 | + print("conn available") |
| 31 | + var conn = server.take_connection() |
| 32 | + print("bytes available ", conn.get_available_bytes()) |
| 33 | + var bs = conn.get_data(conn.get_available_bytes()) |
| 34 | + print("bytes ", bs, len(bs)) |
| 35 | + process_bytes(bs[1]) |
| 36 | + |
| 37 | + |
| 38 | +func process_bytes(bytes): |
| 39 | + var x = 0 |
| 40 | + var y = 0 |
| 41 | + |
| 42 | + for b in bytes: |
| 43 | + print("byte ", b) |
| 44 | + print("xy ", x, " ", y) |
| 45 | + |
| 46 | + tilemap.set_cell(x, y, b == YELLOW) |
| 47 | + |
| 48 | + x += 1 |
| 49 | + if x >= width: |
| 50 | + x = 0 |
| 51 | + y += 1 |
| 52 | + |
| 53 | +func _on_Button_pressed(): |
| 54 | + var cell |
| 55 | + for x in range(4): |
| 56 | + cell = tilemap.get_cell(x, 0) |
| 57 | + print("x ", x, " cell ", cell, |
| 58 | + " type ", typeof(tilemap), " invalid ", cell == TileMap.INVALID_CELL) |
| 59 | + tilemap.set_cell(x, 2, cell) |
| 60 | + |
| 61 | +func _on_teWidth_text_changed(): |
| 62 | + update_width_height() |
| 63 | + |
| 64 | +func _on_teHeight_text_changed(): |
| 65 | + update_width_height() |
| 66 | + |
| 67 | +func update_width_height(): |
| 68 | + print("updating width and height") |
| 69 | + var tHeight = get_node("teHeight").text |
| 70 | + height = int(tHeight) |
| 71 | + var tWidth = get_node("teWidth").text |
| 72 | + width = int(tWidth) |
| 73 | + print(width, " ", height) |
| 74 | + |
0 commit comments