From 29b17954e10c68c082c2ccc8639476d176e498eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Wed, 15 Jul 2020 08:43:13 +0200 Subject: Map : clean up --- Main.gd | 9 +++-- Map.gd | 118 +++++++++++++++++++++++++++++++++------------------------------- 2 files changed, 65 insertions(+), 62 deletions(-) diff --git a/Main.gd b/Main.gd index 88af749..3db3072 100644 --- a/Main.gd +++ b/Main.gd @@ -18,11 +18,12 @@ func _ready(): Map.on_rotate() func on_toggle() -> void: - Map.config(UI.get_node("LOS").pressed, UI.get_node("Move").pressed) + Map.set_mode(UI.get_node("LOS").pressed, UI.get_node("Move").pressed) Map.update() func on_hex_touched(pos : Vector2, hex : Hex, key : int) -> void: - UI.get_node("Info").set_text("\n(%d;%d)\n -> %s\n -> %d" % [int(pos.x), int(pos.y), hex.inspect(), key]) + var s : String = ("offmap" if key == -1 else hex.inspect()) + UI.get_node("Info").set_text("\n(%d;%d)\n -> %s\n -> %d" % [int(pos.x), int(pos.y), s, key]) func _unhandled_input(event : InputEvent) -> void: if event is InputEventMouseMotion: @@ -39,9 +40,7 @@ func _unhandled_input(event : InputEvent) -> void: elif event.button_index == 3: drag_map = event.pressed elif event.button_index == 1: - Map.on_mouse_1(event.pressed) - elif event.button_index == 2: - Map.on_mouse_2(event.pressed) + Map.on_click(event.pressed) elif event is InputEventKey: if event.scancode == KEY_ESCAPE: get_tree().quit() diff --git a/Map.gd b/Map.gd index eb0c056..46890e2 100644 --- a/Map.gd +++ b/Map.gd @@ -33,42 +33,29 @@ func _ready(): board = HexBoard.new() board.tile_factory_fct = funcref(self, "get_tile") board.v = false - drag = null - hexes = {} - los = [] unit = Unit.new() + drag = null func reset() -> void: + los.clear() + move.clear() + short.clear() hexes.clear() hexes[-1] = Hex.new() # off map + p0 = Vector2(0, 0) + p1 = Vector2(3, 3) + $Tank.position = board.center_of(p0) + $Target.position = board.center_of(p1) + for hex in $Hexes.get_children(): + $Hexes.remove_child(hex) + hex.queue_free() -func get_tile(coords : Vector2, k : int) -> Tile: - if hexes.has(k): return hexes[k] - var hex : Hex = Hex.new() - hex.roads = get_road(k) - hex.rotation_degrees = hex_rotation - hex.configure(board.center_of(coords), coords, [GREEN, BLACK, CITY, TREE, MOUNT, BLOCK, MOVE, SHORT]) - hexes[k] = hex - $Hexes.add_child(hex) - return hex - -func get_road(k : int) -> int: - if not board.v: return 0 - var v : int = 0 - v += (HexBoard.Orientation.E if k in [19,20,21,23,24,42,43,44,45,46,47] else 0) - v += (HexBoard.Orientation.W if k in [19,20,21,22,24,25,43,44,45,46,47] else 0) - v += (HexBoard.Orientation.SE if k in [22,32,42,52,62] else 0) - v += (HexBoard.Orientation.NW if k in [32,42,52,62] else 0) - v += (HexBoard.Orientation.NE if k in [7,16,25,32] else 0) - v += (HexBoard.Orientation.SW if k in [7,16,23] else 0) - return v - -func config(l : bool, m : bool) -> void: +func set_mode(l : bool, m : bool) -> void: show_los = l show_move = m + compute() -func on_rotate() -> void: - texture = load(MAPH if board.v else MAPV) +func configure() -> void: var ts : Vector2 = texture.get_size() var v0 : Vector2 = Vector2(50, 100) var c = ts / 2 @@ -86,56 +73,73 @@ func on_rotate() -> void: hex_rotation = 0 board.configure(10, 7, 100, v0, true) emit_signal("configure", c, ts) - p0 = Vector2(0, 0) - p1 = Vector2(3, 3) - $Tank.position = board.center_of(p0) - $Target.position = board.center_of(p1) - for hex in $Hexes.get_children(): - $Hexes.remove_child(hex) - hex.queue_free() reset() - update() + compute() + +func on_rotate() -> void: + texture = load(MAPH if board.v else MAPV) + configure() func on_mouse_move() -> void: if drag != null: drag.position = get_local_mouse_position() -func on_mouse_1(pressed : bool) -> void: +func on_click(pressed : bool) -> void: var pos : Vector2 = get_local_mouse_position() var coords : Vector2 = board.to_map(pos) if pressed: - notify(board.get_tile(coords), pos, coords) - if drag == null: - prev = coords - if board.to_map($Tank.position) == coords: - drag = $Tank - elif board.to_map($Target.position) == coords: - drag = $Target + notify(pos, coords) + prev = coords + if board.to_map($Tank.position) == coords: + drag = $Tank + elif board.to_map($Target.position) == coords: + drag = $Target else: if drag: if board.is_on_map(coords): drag.position = board.center_of(coords) if drag == $Tank: p0 = coords else: p1 = coords - update() + notify(pos, coords) + compute() else: drag.position = board.center_of(prev) - drag = null + drag = null + else: + if coords == prev and board.is_on_map(coords): + change_tile(coords, pos) -func on_mouse_2(pressed : bool) -> void: - var pos : Vector2 = get_local_mouse_position() - var coords : Vector2 = board.to_map(pos) - if pressed: - var hex : Hex = board.get_tile(coords) - hex.change() - notify(hex, pos, coords) - update() +func change_tile(coords : Vector2, pos : Vector2) -> void: + var hex : Hex = board.get_tile(coords) + hex.change() + notify(pos, coords) + compute() + +func get_tile(coords : Vector2, k : int) -> Tile: + if hexes.has(k): return hexes[k] + var hex : Hex = Hex.new() + hex.roads = get_road(k) + hex.rotation_degrees = hex_rotation + hex.configure(board.center_of(coords), coords, [GREEN, BLACK, CITY, TREE, MOUNT, BLOCK, MOVE, SHORT]) + hexes[k] = hex + $Hexes.add_child(hex) + return hex + +func get_road(k : int) -> int: + if not board.v: return 0 + var v : int = 0 + v += (HexBoard.Orientation.E if k in [19,20,21,23,24,42,43,44,45,46,47] else 0) + v += (HexBoard.Orientation.W if k in [19,20,21,22,24,25,43,44,45,46,47] else 0) + v += (HexBoard.Orientation.SE if k in [22,32,42,52,62] else 0) + v += (HexBoard.Orientation.NW if k in [32,42,52,62] else 0) + v += (HexBoard.Orientation.NE if k in [7,16,25,32] else 0) + v += (HexBoard.Orientation.SW if k in [7,16,23] else 0) + return v -func notify(hex : Hex, pos : Vector2, coords : Vector2) -> void: - if board.is_on_map(coords): emit_signal("hex_touched",pos, hex, board.key(coords)) - else: emit_signal("hex_touched", pos, hex, -1) +func notify(pos : Vector2, coords : Vector2) -> void: + emit_signal("hex_touched", pos, board.get_tile(coords), (board.key(coords) if board.is_on_map(coords) else -1)) -func update() -> void: +func compute() -> void: $Los.visible = false for hex in los: hex.show_los(false) if show_los: -- cgit v1.1-2-g2b99