1
Fork 0
mirror of https://github.com/Steffo99/hella-farm.git synced 2024-11-22 08:04:23 +00:00

Specify return type of MainGame.get_ancestor

This commit is contained in:
Steffo 2024-04-14 04:07:35 +02:00
parent 735313b167
commit 1aa785c08a
Signed by: steffo
GPG key ID: 5ADA3868646C3FC0
5 changed files with 15 additions and 29 deletions

View file

@ -9,6 +9,7 @@ signal captured
@export_range(-500, 500, 1) var speed: float = 100.0 @export_range(-500, 500, 1) var speed: float = 100.0
@onready var viewport: Viewport = get_viewport() @onready var viewport: Viewport = get_viewport()
@onready var camera: GameCamera = GameCamera.get_ancestor(self)
enum State { STILL, CAPTURED } enum State { STILL, CAPTURED }
@ -17,7 +18,7 @@ var state: State = State.STILL
func get_relative_mouse_position(): func get_relative_mouse_position():
var viewport_position: Vector2 = viewport.position var camera: Camera
var viewport_mouse_position: Vector2 = viewport.get_mouse_position() var viewport_mouse_position: Vector2 = viewport.get_mouse_position()
var global_mouse_position: Vector2 = viewport_position + viewport_mouse_position var global_mouse_position: Vector2 = viewport_position + viewport_mouse_position
var relative_mouse_position: Vector2 = global_mouse_position - global_position var relative_mouse_position: Vector2 = global_mouse_position - global_position

View file

@ -1,18 +1,7 @@
extends Node2D extends Node2D
class_name Gold
## Emitted when the player hoveres the nugget
signal hovered
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame. func _on_move(movement: Vector2) -> void:
func _process(delta): Log.p(self, "%s" % movement)
pass position += movement
func _on_area_2d_mouse_entered():
hovered.emit()
print("Hovered")

View file

@ -1,21 +1,17 @@
[gd_scene load_steps=4 format=3 uid="uid://uoxwjpmgg27a"] [gd_scene load_steps=4 format=3 uid="uid://uoxwjpmgg27a"]
[ext_resource type="Script" path="res://entities/gold.gd" id="1_lbls1"] [ext_resource type="Script" path="res://entities/gold.gd" id="1_lbls1"]
[ext_resource type="Texture2D" uid="uid://d3o13ebmcux2d" path="res://media/nugget.png" id="2_d322a"] [ext_resource type="PackedScene" uid="uid://bvrxvrjlo5130" path="res://behaviours/move_towards_mouse.tscn" id="2_lso1v"]
[sub_resource type="CircleShape2D" id="CircleShape2D_bjgas"] [sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_1f58t"]
radius = 16.4012
[node name="Gold" type="Node2D"] [node name="Gold" type="Node2D"]
script = ExtResource("1_lbls1") script = ExtResource("1_lbls1")
[node name="Nugget" type="Sprite2D" parent="."] [node name="Nugget" type="Sprite2D" parent="."]
position = Vector2(1, 0) scale = Vector2(12, 12)
texture = ExtResource("2_d322a") texture = SubResource("PlaceholderTexture2D_1f58t")
[node name="Area2D" type="Area2D" parent="."] [node name="MoveTowardsMouse" parent="." instance=ExtResource("2_lso1v")]
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"] [connection signal="move" from="MoveTowardsMouse" to="." method="_on_move"]
shape = SubResource("CircleShape2D_bjgas")
[connection signal="mouse_entered" from="Area2D" to="." method="_on_area_2d_mouse_entered"]

View file

@ -3,7 +3,7 @@ class_name MainGame
## Get the first possible [MainGame] instance by climbing the scene tree one ancestor at a time. ## Get the first possible [MainGame] instance by climbing the scene tree one ancestor at a time.
static func get_ancestor(start: Node): static func get_ancestor(start: Node) -> MainGame:
var current = start var current = start
while current is Node: while current is Node:
if current is MainGame: if current is MainGame:

View file

@ -1,6 +1,6 @@
[gd_scene load_steps=5 format=3 uid="uid://cxj5aud02f40j"] [gd_scene load_steps=5 format=3 uid="uid://cxj5aud02f40j"]
[ext_resource type="Script" path="res://scenes/game/menu_game.gd" id="1_vrqkh"] [ext_resource type="Script" path="res://scenes/game/menu_game.gd" id="1_x18yb"]
[ext_resource type="PackedScene" uid="uid://brvbtvt4em32" path="res://behaviours/counter.tscn" id="2_7flt7"] [ext_resource type="PackedScene" uid="uid://brvbtvt4em32" path="res://behaviours/counter.tscn" id="2_7flt7"]
[sub_resource type="TileSet" id="TileSet_g2dkm"] [sub_resource type="TileSet" id="TileSet_g2dkm"]
@ -9,7 +9,7 @@
size = Vector2(1280, 720) size = Vector2(1280, 720)
[node name="MainGame" type="Node2D"] [node name="MainGame" type="Node2D"]
script = ExtResource("1_vrqkh") script = ExtResource("1_x18yb")
[node name="TileMap" type="TileMap" parent="."] [node name="TileMap" type="TileMap" parent="."]
tile_set = SubResource("TileSet_g2dkm") tile_set = SubResource("TileSet_g2dkm")