summaryrefslogtreecommitdiffstats
path: root/Map.gd
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2020-07-15 08:43:13 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2020-07-15 08:43:13 +0200
commit29b17954e10c68c082c2ccc8639476d176e498eb (patch)
treee8267291d31899cfb6cccef7e50e4c3c35d29d76 /Map.gd
parent1c1e1caa39a94deb1bfaccea5c15662fdb3a8605 (diff)
downloadgodot-hexgrid-29b17954e10c68c082c2ccc8639476d176e498eb.zip
godot-hexgrid-29b17954e10c68c082c2ccc8639476d176e498eb.tar.gz
Map : clean up
Diffstat (limited to 'Map.gd')
-rw-r--r--Map.gd118
1 files changed, 61 insertions, 57 deletions
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: