From 4597189f157834c80f56b12b701fd2b2a15c2798 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Sun, 8 Sep 2024 22:35:06 +0200 Subject: next commit --- Mob.gd | 208 ----------------------------------------------------------------- 1 file changed, 208 deletions(-) delete mode 100644 Mob.gd (limited to 'Mob.gd') diff --git a/Mob.gd b/Mob.gd deleted file mode 100644 index 3f1a00e..0000000 --- a/Mob.gd +++ /dev/null @@ -1,208 +0,0 @@ -class_name Mob -extends CharacterBody2D - - -signal selected - -static var selected_unit: Mob - -var is_selected = false : - set(value): - if value: - Mob.selected_unit = self - selected.emit() - $Label.visible = true - line.visible = true - else: - if Mob.selected_unit == self: - Mob.selected_unit = null - $Label.visible = false - line.visible = false - is_selected = value - queue_redraw() - -var is_hovered = false : - set(value): - is_hovered = value - queue_redraw() - -var target: Node2D : - set(value): - target = value - reset_path() -var immediate_target: Vector2 - -var current_path: PackedVector2Array -var current_path_idx = 0 : - set(value): - current_path_idx = value - if line: - line.points = PackedVector2Array(current_path.slice(value)) - -var previous_path: PackedVector2Array -var previous_path_idx = 0 - -var previous_position: Vector2 - -var recent_closest_paths: Array[PackedVector2Array] - -var roaming_mode = false - -@onready var line: Line2D = $MobPathLine.duplicate() - -@export var speed: float = 100 - -@export var hp = 50 - - -# Called when the node enters the scene tree for the first time. -func _ready(): - if not target: - target = get_tree().current_scene.find_child("Goal") - - get_tree().current_scene.add_child(line) - - reset_path() - Game.path_grid_changed.connect(func(): - reset_path() - current_path_idx = min(1, current_path.size() - 1) - ) - - %HPBar.init(hp) - set_hp(hp) - - $SelectionArea/CollisionShape2D.shape.size = $Sprite2D.texture.get_size() * $Sprite2D.scale - - -func _physics_process(delta): - previous_position = global_position - - if not current_path.is_empty(): - if (global_position - current_path[current_path_idx]).is_zero_approx(): - current_path_idx += 1 - if current_path_idx >= current_path.size(): - reset_path() - - walk_along_path(current_path, current_path_idx, delta) - - if roaming_mode: - var collision = get_last_slide_collision() - if collision: - var tower = collision.get_collider() as Node2D - Game.destroy_tower(tower) - - # if unit stuck in tower - if previous_position == global_position: # doesn't work with 0.05 fluctuating position - reset_path() - roaming_mode = true - - -func _draw(): - if is_selected: - draw_circle( - Vector2.ZERO, - Game.map.tile_set.tile_size.x * 0.75, - Color(1, 1, 1, 0.75), - false, - 1.0 - ) - modulate = Color(1.5, 1.5, 1.5) - elif is_hovered: - draw_circle( - Vector2.ZERO, - Game.map.tile_set.tile_size.x * 0.75, - Color(1, 1, 1, 0.5), - false, - 1.0 - ) - modulate = Color(1.25, 1.25, 1.25) - else: - modulate = Color(1, 1, 1) - - -func _on_navigation_base_area_entered(area: Area2D): - if area.is_in_group("goal"): - Game.score += 1 - queue_free() - if area.is_in_group("path"): - var path_node = area.get_parent() - if path_node == target: - target = path_node.next_node - - -func walk_along_path(path: PackedVector2Array, index: int, delta: float): - immediate_target = path[index] - var displacement := (path[index]) - global_position - var direction := displacement.normalized() - var distance := displacement.length() - - var max_speed: float = (distance / delta) - velocity = direction * minf(speed, max_speed) - move_and_slide() - - -func set_hp(value): - hp = value - %HPBar.set_value(value) - - $Label.text = str(hp) - - if hp <= 0: - queue_free() - - -func reset_path(): - roaming_mode = false - current_path = get_grid_path() - - if current_path.is_empty(): - current_path = get_grid_path(true) - recent_closest_paths.append(current_path) - - # reached end of partial path - if current_path.size() == 1 and current_path[0] == global_position: - roaming_mode = true - current_path = PackedVector2Array([target.path_position + Vector2(16,16)]) - - # iterating between one or more closest paths - elif recent_closest_paths.count(current_path) >= 2: - roaming_mode = true - current_path = PackedVector2Array([target.path_position + Vector2(16,16)]) - recent_closest_paths = [] - else: - recent_closest_paths = [] - roaming_mode = false - - current_path_idx = 0 - - if line: - line.points = PackedVector2Array(current_path) - - -func get_grid_path(partial = false): - return Game.path_grid.get_point_path( - Game.map.local_to_map(global_position), - Game.map.local_to_map(target.path_position), - partial - ) - - -func _on_selection_area_input_event(_viewport: Node, event: InputEvent, _shape_idx: int) -> void: - if Game.mode == Game.Mode.DEFAULT: - if event.is_action_pressed("select"): - if selected_unit: - selected_unit.is_selected = false - is_selected = true - $Label.text = str(hp) - - -func _on_selection_area_mouse_entered() -> void: - is_hovered = true - -func _on_selection_area_mouse_exited() -> void: - is_hovered = false - - -func _on_tree_exiting() -> void: - is_selected = false - line.queue_free() -- cgit v1.2.3