From df57b3be4e9a8c3ed3d5d14b4b4f1e2ef7629cd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Thu, 9 Jul 2020 17:03:28 +0200 Subject: implement different terrain type, use elevation, and height to compute LOS --- Hex.gd | 51 ++++++++++++++++++++++++++++++++++++++++----------- Main.gd | 6 +++--- Main.tscn | 4 ++-- Map.gd | 24 +++++++++++------------- assets/block.png | Bin 5029 -> 0 bytes assets/city.png | Bin 0 -> 14508 bytes assets/mountain.png | Bin 0 -> 13386 bytes assets/tree.png | Bin 0 -> 13687 bytes 8 files changed, 56 insertions(+), 29 deletions(-) delete mode 100644 assets/block.png create mode 100644 assets/city.png create mode 100644 assets/mountain.png create mode 100644 assets/tree.png diff --git a/Hex.gd b/Hex.gd index f06c7bf..f6b615d 100644 --- a/Hex.gd +++ b/Hex.gd @@ -3,22 +3,51 @@ extends Tile class_name Hex, "res://godot/Tile.png" +var type : int = 0 + +func _ready() -> void: + type = -1 + func inspect() -> String: - return "[%d;%d] %s" % [coords.x,coords.y,not blocked] + var s : String = 'plain' + if type == 2: s = 'city' + elif type == 3: s = 'wood' + elif type == 4: s = 'mountain' + return "%s e:%d h:%d c:%d\n -> [%d;%d]\n -> (%d;%d)" % [s, elevation(), height(), cost(), coords.x, coords.y, position.x, position.y] + +func change() -> void: + type += 1 + if type < 2: type = 2 + if type > 4: type = -1 + for i in range(2, 5): + enable_overlay(i, i == type) -func block(b : bool) -> void: - enable_overlay(0, b) +func cost() -> int: + if type == -1: return 1 + return type - 1 -func is_blocked() -> bool: - return is_overlay_on(0) +func height() -> int: + if type == 2: return 2 + elif type == 3: return 1 + elif type == 4: return 0 + return 0 + +func elevation() -> int: + if type == 4: return 2 + return 0 func block_los(from : Tile, to : Tile, d : float, dt : float) -> bool: - return is_blocked() + var h : int = height() + elevation() + if h == 0: return false + var e : int = from.elevation() + if e > h: + if to.elevation() > h: return false + return (h * dt / (e - h)) >= (d - dt) + h -= e + return ((h * d / dt) >= to.elevation() - e) func show_los(b) -> void: - if not b: - enable_overlay(1, false) - enable_overlay(2, false) + if b: enable_overlay((1 if blocked else 0), true) else: - if blocked: enable_overlay(2, true) - else: enable_overlay(1, true) + enable_overlay(0, false) + enable_overlay(1, false) diff --git a/Main.gd b/Main.gd index 7de0300..36affaa 100644 --- a/Main.gd +++ b/Main.gd @@ -10,12 +10,12 @@ onready var Camera : Camera2D = $ViewportContainer/Viewport/Camera func _ready(): UI.get_node("rotate").connect("pressed", Map, "on_rotate") Map.connect("configure", Camera, "on_configure") - Map.connect("touched", self, "on_touched") + Map.connect("hex_touched", self, "on_hex_touched") Camera.window = $ViewportContainer/Viewport.size Map.on_rotate() -func on_touched(s : String) -> void: - UI.get_node("Label").set_text(s) +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]) func _unhandled_input(event : InputEvent) -> void: if event is InputEventMouseMotion: diff --git a/Main.tscn b/Main.tscn index fcecad8..74aed48 100644 --- a/Main.tscn +++ b/Main.tscn @@ -22,9 +22,9 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="Label" type="Label" parent="UI"] +[node name="Info" type="Label" parent="UI"] margin_left = 2.0 -margin_top = 477.0 +margin_top = 501.0 margin_right = 131.0 margin_bottom = 598.0 __meta__ = { diff --git a/Map.gd b/Map.gd index 1300304..76edb86 100644 --- a/Map.gd +++ b/Map.gd @@ -1,13 +1,16 @@ extends Sprite signal configure(center, texture_size) -signal touched(msg) +signal hex_touched(pos, hex, key) const MAPH : String = "res://assets/map-h.png" const MAPV : String = "res://assets/map-v.png" const BLOCK : String = "res://assets/block.png" const BLACK : String = "res://assets/black.png" const GREEN : String = "res://assets/green.png" +const TREE : String = "res://assets/tree.png" +const CITY : String = "res://assets/city.png" +const MOUNT : String = "res://assets/mountain.png" var drag : Sprite @@ -35,7 +38,7 @@ func get_tile(coords : Vector2, k : int) -> Tile: if hexes.has(k): return hexes[k] var hex : Hex = Hex.new() hex.rotation_degrees = hex_rotation - hex.configure(board.center_of(coords), coords, [BLOCK, GREEN, BLACK]) + hex.configure(board.center_of(coords), coords, [GREEN, BLACK, CITY, TREE, MOUNT]) hexes[k] = hex $Hexes.add_child(hex) return hex @@ -77,7 +80,7 @@ func on_mouse_1(pressed : bool) -> void: var pos : Vector2 = get_local_mouse_position() var coords : Vector2 = board.to_map(pos) if pressed: - notify(pos, coords) + notify(board.get_tile(coords), pos, coords) if drag == null: prev = coords if board.to_map($Tank.position) == coords: @@ -99,19 +102,14 @@ func on_mouse_2(pressed : bool) -> void: var pos : Vector2 = get_local_mouse_position() var coords : Vector2 = board.to_map(pos) if pressed: - notify(pos, coords) var hex : Hex = board.get_tile(coords) - if not hex.is_blocked(): hex.block(true) - else: hex.block(false) + hex.change() + notify(hex, pos, coords) update_los() -func notify(pos : Vector2, coords : Vector2) -> void: - if board.is_on_map(coords): - var center : Vector2 = board.center_of(coords) - var key : int = board.key(coords) - emit_signal("touched","%s\n -> %s\n -> %s\n -> %d" % [pos, coords, center, key]) - else: - emit_signal("touched", "off board") +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 update_los() -> void: for hex in los: diff --git a/assets/block.png b/assets/block.png deleted file mode 100644 index 7704d65..0000000 Binary files a/assets/block.png and /dev/null differ diff --git a/assets/city.png b/assets/city.png new file mode 100644 index 0000000..35ef9a3 Binary files /dev/null and b/assets/city.png differ diff --git a/assets/mountain.png b/assets/mountain.png new file mode 100644 index 0000000..ebc224b Binary files /dev/null and b/assets/mountain.png differ diff --git a/assets/tree.png b/assets/tree.png new file mode 100644 index 0000000..b3300a4 Binary files /dev/null and b/assets/tree.png differ -- cgit v1.1-2-g2b99