PirateTreasureHunt/GameScene.gd

72 lines
2.1 KiB
GDScript
Raw Normal View History

2021-06-12 17:48:58 +02:00
extends Node2D
var Player
var Level
var GameCamera
var DebugLayer
2021-06-12 23:42:38 +02:00
var BackgroundWater
2021-06-12 17:48:58 +02:00
var NavTarget
var DebugLabel
var DigSite
var DiggedSites = {}
2021-06-12 23:42:38 +02:00
var ChestLocations = {}
2021-06-12 17:48:58 +02:00
# Called when the node enters the scene tree for the first time.
func _ready():
Player = get_node("Player")
Level = get_node("Level")
GameCamera = get_node("GameCamera")
DebugLayer = get_node("DebugLayer")
NavTarget = get_node("DebugLayer/NavTarget")
2021-06-12 23:42:38 +02:00
BackgroundWater = get_node("GameCamera/BackgroundWater")
2021-06-12 17:48:58 +02:00
DebugLabel = GameCamera.get_node("Widgets/HBoxContainer/DebugLabel")
DigSite = preload("res://DigSite.tscn")
Globals.DebugLabel = DebugLabel
2021-06-12 23:42:38 +02:00
ChestLocations[Vector2(3,3)] = true
2021-06-12 17:48:58 +02:00
Player.connect("DigSiteReached", self, "handle_dig_site_reached")
Player.connect("DigStop", self, "handle_dig_stop")
func handle_dig_site_reached(dig_grid_coords):
if not DiggedSites.has(dig_grid_coords):
print ("Starting digging at ", dig_grid_coords)
2021-06-12 23:42:38 +02:00
Player.CurrentDigGridCoords = dig_grid_coords
2021-06-12 17:48:58 +02:00
Player.start_dig()
func handle_dig_stop():
print ("Stopping digging")
2021-06-12 23:42:38 +02:00
var dig_grid_coords = Player.CurrentDigGridCoords
if dig_grid_coords == null:
print ("Error: handling stop dig but have no dig coords!")
return
var has_treasure = ChestLocations.has(dig_grid_coords)
DiggedSites[dig_grid_coords] = true
var new_digsite = DigSite.instance()
new_digsite.HasTreasure = has_treasure
new_digsite.transform.origin = Globals.GridCenterToWorldCoord(dig_grid_coords)
Level.add_child(new_digsite)
Player.CurrentDigGridCoords = null
2021-06-12 17:48:58 +02:00
func _process(delta):
Globals.ClearDebugLabel()
2021-06-12 23:42:38 +02:00
var screen_size = OS.get_window_safe_area().size
GameCamera.transform.origin = Player.transform.origin - screen_size * 0
BackgroundWater.world_offset = GameCamera.transform.origin
2021-06-12 17:48:58 +02:00
func _input(event):
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT and event.pressed:
var event_world_pos = GameCamera.screenToWorld(event.position)
NavTarget.transform.origin = event_world_pos
Player.NavTarget = Globals.WorldToGridCenter(event_world_pos)
DebugLabel.text = str(event_world_pos)
if event.button_index == BUTTON_WHEEL_UP and event.pressed:
print("Wheel up")