143 lines
4 KiB
GDScript
143 lines
4 KiB
GDScript
extends StaticBody2D
|
|
|
|
class_name Base
|
|
|
|
enum DockState { Ready, Process, Busy, Leave }
|
|
|
|
signal dock_requested
|
|
|
|
## Reference to star system
|
|
@onready var star_system: StarSystem = get_tree().current_scene
|
|
## Physical gate
|
|
@onready var gate_static = $Gate
|
|
## Logical gate for docking
|
|
@onready var gate_area = $GateArea
|
|
## Docking area
|
|
@onready var dock_area = $DockingArea
|
|
|
|
## Menu which will instantiate on docking
|
|
@export var menu: PackedScene
|
|
## Faction that this base represents
|
|
@export var faction: Game.Faction
|
|
|
|
## List of items that this base sells
|
|
@export var items_on_sell: Array[Item]
|
|
## List of items that this base buys
|
|
@export var items_on_buy: Array[Item]
|
|
|
|
## Quest that is given on this base
|
|
@export var quest: Quest
|
|
|
|
## List of prices for items that this base sells
|
|
var sell_prices: Array[float]
|
|
## List of prices for items that this base buys
|
|
var buy_prices: Array[float]
|
|
|
|
## Decides whether ship is docked or in process
|
|
var dock_state: DockState = DockState.Ready
|
|
|
|
var touching_gate = false
|
|
var touching_dock = false
|
|
|
|
var player_ship = null
|
|
|
|
func _ready():
|
|
mouse_entered.connect(star_system.set_targeted_node.bind(self))
|
|
mouse_exited.connect(star_system.clear_targeted_node)
|
|
gate_area.body_entered.connect(gate_area_body_entered)
|
|
gate_area.body_exited.connect(gate_area_body_exited)
|
|
dock_area.body_entered.connect(dock_area_body_entered)
|
|
dock_area.body_exited.connect(dock_area_body_exited)
|
|
dock_requested.connect(on_dock_requested)
|
|
dock_ready()
|
|
generate_prices()
|
|
|
|
## Switches dock state
|
|
func on_dock_requested():
|
|
player_ship = star_system.player_ship
|
|
match dock_state:
|
|
DockState.Ready:
|
|
dock_process()
|
|
DockState.Process:
|
|
dock_ready()
|
|
|
|
func gate_area_body_entered(body):
|
|
if body.ship is PlayerShip:
|
|
touching_gate = true
|
|
|
|
func gate_area_body_exited(body):
|
|
if body.ship is PlayerShip:
|
|
touching_gate = false
|
|
|
|
func dock_area_body_entered(body):
|
|
if body.ship is PlayerShip:
|
|
touching_dock = true
|
|
|
|
func dock_area_body_exited(body):
|
|
if body.ship is PlayerShip:
|
|
touching_dock = false
|
|
|
|
func _process(_delta):
|
|
if dock_state == DockState.Process:
|
|
var distance_to_player = global_position.distance_to(player_ship.global_position)
|
|
if touching_dock and !touching_gate:
|
|
dock_busy()
|
|
if !touching_dock and !touching_gate and distance_to_player > 2048:
|
|
dock_ready()
|
|
if dock_state == DockState.Leave:
|
|
if !touching_dock and !touching_gate:
|
|
dock_ready()
|
|
|
|
## Sets dock state to Ready
|
|
func dock_ready():
|
|
dock_state = DockState.Ready
|
|
enable_gate()
|
|
## Sets dock state to Process
|
|
func dock_process():
|
|
dock_state = DockState.Process
|
|
disable_gate()
|
|
## Sets dock state to Busy
|
|
func dock_busy():
|
|
dock_state = DockState.Busy
|
|
enable_gate()
|
|
# opening base menu
|
|
var menu_instance = menu.instantiate()
|
|
menu_instance.base = self
|
|
player_ship.non_colorable_gui.add_child(menu_instance)
|
|
## Sets dock state to Leave
|
|
func dock_leave():
|
|
dock_state = DockState.Leave
|
|
disable_gate()
|
|
## Shows gate
|
|
func enable_gate():
|
|
gate_static.visible = true
|
|
gate_static.process_mode = Node.PROCESS_MODE_INHERIT
|
|
gate_area.visible = false
|
|
dock_area.visible = false
|
|
## Hides gate so base is accessible to enter/exit
|
|
func disable_gate():
|
|
gate_static.visible = false
|
|
gate_static.process_mode = Node.PROCESS_MODE_DISABLED
|
|
gate_area.visible = true
|
|
dock_area.visible = true
|
|
|
|
## Generates prices for valuable items
|
|
func generate_prices():
|
|
# gen prices for buyable items
|
|
for i in range(len(items_on_buy)):
|
|
var item = items_on_buy[i]
|
|
var avg = (item.max_price + item.min_price) * 50
|
|
if item in items_on_sell:
|
|
# buy not higher than avg price
|
|
buy_prices.append(randi_range(item.min_price * 100, avg) / 100.0)
|
|
else:
|
|
buy_prices.append(randi_range(item.min_price * 100, item.max_price * 100) / 100.0)
|
|
# gen prices for items in sell
|
|
for i in range(len(items_on_sell)):
|
|
var item = items_on_sell[i]
|
|
var avg = (item.max_price + item.min_price) * 50
|
|
if item in items_on_buy:
|
|
# sell not lower than avg price
|
|
sell_prices.append(randi_range(avg, item.max_price * 100) / 100.0)
|
|
else:
|
|
sell_prices.append(randi_range(item.min_price * 100, item.max_price * 100) / 100.0)
|