From 4172b336c14d5ab78151defca6bf321c72bd6d71 Mon Sep 17 00:00:00 2001 From: 2ndbeam <166764593+2ndbeam@users.noreply.github.com> Date: Tue, 30 Apr 2024 16:22:55 +0300 Subject: [PATCH] Rewriting ships: Added some player interface and started rewriting star system --- project.godot | 5 + scenes/MainMenu.tscn | 2 +- scenes/Ships/player_ship.tscn | 145 +++++++++++++++++- scenes/Space.tscn | 4 +- .../Required Scenes}/StarsController.tscn | 4 +- scenes/Star Systems/star_system_template.tscn | 29 ++++ scenes/debug.tscn | 107 +------------ scripts/Ship/hull.gd | 4 + scripts/Ship/player_camera.gd | 16 ++ scripts/Ship/player_input_controller.gd | 1 + scripts/Ship/player_ship.gd | 9 ++ scripts/Ship/ship.gd | 10 +- scripts/{Space.gd => Spaceold.gd} | 0 scripts/misc/ProjectilesContainer.gd | 5 +- scripts/misc/StarsGeneration.gd | 18 --- scripts/misc/stars_generator.gd | 28 ++++ scripts/nebula.gd | 18 ++- scripts/objects/Star.gd | 1 + scripts/star_system.gd | 17 ++ shaders/materials/nebula_shader.tres | 16 ++ 20 files changed, 300 insertions(+), 139 deletions(-) rename scenes/{ => Star Systems/Required Scenes}/StarsController.tscn (84%) create mode 100644 scenes/Star Systems/star_system_template.tscn create mode 100644 scripts/Ship/player_camera.gd create mode 100644 scripts/Ship/player_ship.gd rename scripts/{Space.gd => Spaceold.gd} (100%) delete mode 100644 scripts/misc/StarsGeneration.gd create mode 100644 scripts/misc/stars_generator.gd create mode 100644 scripts/star_system.gd create mode 100644 shaders/materials/nebula_shader.tres diff --git a/project.godot b/project.godot index 3d5bef7..ccf07be 100644 --- a/project.godot +++ b/project.godot @@ -16,6 +16,11 @@ run/main_scene="res://scenes/MainMenu.tscn" config/features=PackedStringArray("4.2", "Forward Plus") config/icon="res://icon.svg" +[debug] + +gdscript/warnings/shadowed_variable=0 +gdscript/warnings/integer_division=0 + [display] window/size/viewport_width=1280 diff --git a/scenes/MainMenu.tscn b/scenes/MainMenu.tscn index 491f695..b318b9d 100644 --- a/scenes/MainMenu.tscn +++ b/scenes/MainMenu.tscn @@ -3,7 +3,7 @@ [ext_resource type="Script" path="res://scripts/menu/MainMenu.gd" id="1_2wax0"] [ext_resource type="Shader" uid="uid://f6lhks6rp5jw" path="res://shaders/testicles.tres" id="1_on8wy"] [ext_resource type="Gradient" uid="uid://c6bcjydbwm5id" path="res://scenes/SpaceGradient.tres" id="2_7racd"] -[ext_resource type="PackedScene" uid="uid://dpggye27ln436" path="res://scenes/StarsController.tscn" id="4_g7254"] +[ext_resource type="PackedScene" uid="uid://dpggye27ln436" path="res://scenes/Star Systems/Required Scenes/StarsController.tscn" id="4_g7254"] [ext_resource type="Script" path="res://scripts/menu/MainMenuButton.gd" id="5_q6x10"] [sub_resource type="FastNoiseLite" id="FastNoiseLite_rtgkw"] diff --git a/scenes/Ships/player_ship.tscn b/scenes/Ships/player_ship.tscn index 9b1b2d6..249844d 100644 --- a/scenes/Ships/player_ship.tscn +++ b/scenes/Ships/player_ship.tscn @@ -1,13 +1,156 @@ -[gd_scene load_steps=4 format=3 uid="uid://dok3i8u5t1ka4"] +[gd_scene load_steps=12 format=3 uid="uid://dok3i8u5t1ka4"] [ext_resource type="PackedScene" uid="uid://bjkshql8ut6hk" path="res://scenes/Ships/ship.tscn" id="1_6x7bu"] +[ext_resource type="Script" path="res://scripts/Ship/player_ship.gd" id="2_oqdd7"] [ext_resource type="Script" path="res://scripts/Ship/player_input_controller.gd" id="3_0e84a"] [ext_resource type="PackedScene" uid="uid://ryy1tdrxmjav" path="res://scenes/Ships/Modules/Weapons/weapon.tscn" id="4_fy1be"] +[ext_resource type="Script" path="res://scripts/Ship/player_camera.gd" id="5_rclap"] + +[sub_resource type="GDScript" id="GDScript_ry4sc"] +resource_name = "money_counter" +script/source = "extends Label + +## Text with placeholder +@onready var unformatted_text: String = text +## Shortcut to get_parent().get_parent() +@onready var ship: PlayerShip = get_parent().get_parent() + +func _process(_delta): + text = unformatted_text % ship.money +" + +[sub_resource type="GDScript" id="GDScript_vko7a"] +resource_name = "velocity_counter" +script/source = "extends Label + +## Text with placeholder +@onready var unformatted_text: String = text +## Shortcut to get_parent().get_parent() +@onready var ship: PlayerShip = get_parent().get_parent() + +func _process(_delta): + text = unformatted_text % ship.hull.scalar_velocity +" + +[sub_resource type="GDScript" id="GDScript_uoaip"] +resource_name = "health_counter" +script/source = "extends Label + +## Text with placeholder +@onready var unformatted_text: String = text +## Shortcut to get_parent().get_parent() +@onready var ship: PlayerShip = get_parent().get_parent() + +func _process(_delta): + text = unformatted_text.format([ship.hull.hp, ship.hull.max_hp]) +" + +[sub_resource type="GDScript" id="GDScript_q1sx2"] +resource_name = "shield_counter" +script/source = "extends Label + +## Text with placeholder +@onready var unformatted_text: String = text +## Shortcut to get_parent().get_parent() +@onready var ship: PlayerShip = get_parent().get_parent() + +func _process(_delta): + text = unformatted_text.format([ship.shield.capacity, ship.shield.max_capacity]) +" + +[sub_resource type="Theme" id="Theme_d8vjy"] +default_font_size = 48 + +[sub_resource type="GDScript" id="GDScript_6uw1d"] +resource_name = "pause_controller" +script/source = "extends Control + +# TODO: implement save + +## Resumes game +func _on_resume_button_up(): + get_tree().current_scene.unpause() + +## Saves profile and exits game +func _on_exit_button_up(): + get_tree().current_scene.unpause() + get_tree().change_scene_to_file(\"res://scenes/MainMenu.tscn\") + #Game.profile_save(get_tree().current_scene) +" [node name="PlayerShip" instance=ExtResource("1_6x7bu")] +script = ExtResource("2_oqdd7") [node name="InputController" type="Node2D" parent="." index="0"] script = ExtResource("3_0e84a") [node name="Weapon" parent="Weapons" index="0" instance=ExtResource("4_fy1be")] action_id = "primary" + +[node name="ColorableGUI" type="CanvasLayer" parent="." index="5"] + +[node name="Money" type="Label" parent="ColorableGUI" index="0"] +offset_left = 7.0 +offset_top = 611.0 +offset_right = 92.0 +offset_bottom = 634.0 +text = "Money: %d" +script = SubResource("GDScript_ry4sc") + +[node name="Velocity" type="Label" parent="ColorableGUI" index="1"] +offset_left = 7.0 +offset_top = 688.0 +offset_right = 137.0 +offset_bottom = 711.0 +text = "Velocity: %d px/s" +script = SubResource("GDScript_vko7a") + +[node name="Health" type="Label" parent="ColorableGUI" index="2"] +offset_left = 7.0 +offset_top = 663.0 +offset_right = 168.0 +offset_bottom = 686.0 +text = "Health: {0} / {1} units" +script = SubResource("GDScript_uoaip") + +[node name="Shield" type="Label" parent="ColorableGUI" index="3"] +offset_left = 8.0 +offset_top = 638.0 +offset_right = 164.0 +offset_bottom = 661.0 +text = "Shield: {0} / {1} units" +script = SubResource("GDScript_q1sx2") + +[node name="PauseController" type="Control" parent="ColorableGUI" index="4"] +process_mode = 2 +layout_mode = 3 +anchors_preset = 0 +offset_right = 40.0 +offset_bottom = 40.0 +theme = SubResource("Theme_d8vjy") +script = SubResource("GDScript_6uw1d") + +[node name="Resume" type="Button" parent="ColorableGUI/PauseController" index="0"] +layout_mode = 0 +offset_left = 444.0 +offset_top = 254.0 +offset_right = 779.0 +offset_bottom = 329.0 +text = "Resume game" + +[node name="Exit" type="Button" parent="ColorableGUI/PauseController" index="1"] +layout_mode = 0 +offset_left = 449.0 +offset_top = 357.0 +offset_right = 763.0 +offset_bottom = 432.0 +text = "Save and exit" + +[node name="NonColorableGUI" type="CanvasLayer" parent="." index="6"] + +[node name="Camera" type="Camera2D" parent="." index="7"] +zoom = Vector2(0.5, 0.5) +script = ExtResource("5_rclap") + +[connection signal="button_up" from="ColorableGUI/PauseController/Resume" to="ColorableGUI/PauseController" method="_on_resume_button_up"] +[connection signal="button_up" from="ColorableGUI/PauseController/Exit" to="ColorableGUI/PauseController" method="_on_exit_button_up"] diff --git a/scenes/Space.tscn b/scenes/Space.tscn index 1be618e..9bac2f4 100644 --- a/scenes/Space.tscn +++ b/scenes/Space.tscn @@ -1,8 +1,8 @@ [gd_scene load_steps=31 format=3 uid="uid://14k35mkjwi5i"] [ext_resource type="PackedScene" uid="uid://ccrs28h3b2tfy" path="res://scenes/MainShip.tscn" id="1_6fvpc"] -[ext_resource type="Script" path="res://scripts/Space.gd" id="1_ppaw3"] -[ext_resource type="PackedScene" uid="uid://dpggye27ln436" path="res://scenes/StarsController.tscn" id="3_jbyyq"] +[ext_resource type="Script" path="res://scripts/Spaceold.gd" id="1_ppaw3"] +[ext_resource type="PackedScene" uid="uid://dpggye27ln436" path="res://scenes/Star Systems/Required Scenes/StarsController.tscn" id="3_jbyyq"] [ext_resource type="Script" path="res://scripts/misc/ProjectilesContainer.gd" id="4_dtv2c"] [ext_resource type="PackedScene" uid="uid://dbtrc26016xov" path="res://scenes/Base.tscn" id="5_bjt5p"] [ext_resource type="PackedScene" uid="uid://523dme3h6d6c" path="res://scenes/npcships/NPCShipDefault.tscn" id="6_67746"] diff --git a/scenes/StarsController.tscn b/scenes/Star Systems/Required Scenes/StarsController.tscn similarity index 84% rename from scenes/StarsController.tscn rename to scenes/Star Systems/Required Scenes/StarsController.tscn index b6d56ae..c2bb3ad 100644 --- a/scenes/StarsController.tscn +++ b/scenes/Star Systems/Required Scenes/StarsController.tscn @@ -1,13 +1,13 @@ [gd_scene load_steps=3 format=3 uid="uid://dpggye27ln436"] -[ext_resource type="Script" path="res://scripts/misc/StarsGeneration.gd" id="1_rcdwc"] +[ext_resource type="Script" path="res://scripts/misc/stars_generator.gd" id="1_uxmpu"] [ext_resource type="PackedScene" uid="uid://kyuhwil8vq7n" path="res://scenes/Star.tscn" id="2_ypr5c"] [node name="Stars" type="ParallaxBackground"] process_mode = 1 layer = -5 scroll_ignore_camera_zoom = true -script = ExtResource("1_rcdwc") +script = ExtResource("1_uxmpu") star = ExtResource("2_ypr5c") [node name="CloseStars" type="ParallaxLayer" parent="."] diff --git a/scenes/Star Systems/star_system_template.tscn b/scenes/Star Systems/star_system_template.tscn new file mode 100644 index 0000000..40bbd92 --- /dev/null +++ b/scenes/Star Systems/star_system_template.tscn @@ -0,0 +1,29 @@ +[gd_scene load_steps=6 format=3 uid="uid://bsnrcw64qr2hr"] + +[ext_resource type="Script" path="res://scripts/star_system.gd" id="1_xx8w2"] +[ext_resource type="PackedScene" uid="uid://dpggye27ln436" path="res://scenes/Star Systems/Required Scenes/StarsController.tscn" id="2_iqrn0"] +[ext_resource type="Script" path="res://scripts/misc/ProjectilesContainer.gd" id="3_h5cm3"] +[ext_resource type="Material" uid="uid://bawhivm5cr2w" path="res://shaders/materials/nebula_shader.tres" id="4_o0ld7"] +[ext_resource type="Script" path="res://scripts/nebula.gd" id="5_la84n"] + +[node name="StarSystem" type="Node"] +script = ExtResource("1_xx8w2") +width = 1280 +height = 720 + +[node name="Stars" parent="." instance=ExtResource("2_iqrn0")] + +[node name="ProjectileContainer" type="Node" parent="."] +script = ExtResource("3_h5cm3") + +[node name="Background" type="CanvasLayer" parent="."] + +[node name="Nebula" type="ColorRect" parent="Background"] +material = ExtResource("4_o0ld7") +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +script = ExtResource("5_la84n") +color_background = Color(0.36, 0.18612, 0.1116, 1) diff --git a/scenes/debug.tscn b/scenes/debug.tscn index bd64003..3e3c713 100644 --- a/scenes/debug.tscn +++ b/scenes/debug.tscn @@ -1,95 +1,11 @@ [gd_scene load_steps=6 format=3 uid="uid://ckdttdkoo1t0s"] [ext_resource type="PackedScene" uid="uid://dok3i8u5t1ka4" path="res://scenes/Ships/player_ship.tscn" id="1_b1l1c"] +[ext_resource type="Script" path="res://scripts/star_system.gd" id="1_du5mm"] [ext_resource type="Shader" path="res://shaders/ship.gdshader" id="2_15op6"] [ext_resource type="Script" path="res://scripts/misc/ProjectilesContainer.gd" id="3_amh71"] -[sub_resource type="GDScript" id="GDScript_6w8qb"] -resource_name = "gogo" -script/source = "extends Node2D - -@onready var items = $Items -@onready var verified = $\"Verified Items\" - -# Called when the node enters the scene tree for the first time. -func _ready(): - var dir = DirAccess.open(\"res://items\") - var files = dir.get_files() - var text = \"Items:\\n\" - for file in files: - text += \"{file}\\n\".format({'file': file}) - text += \"Hulls:\\n\" - dir = DirAccess.open(\"res://scenes/hulls\") - files = dir.get_files() - for file in files: - text += \"{file}\\n\".format({'file': file}) - text += \"Engines:\\n\" - dir = DirAccess.open(\"res://scenes/engines\") - files = dir.get_files() - for file in files: - text += \"{file}\\n\".format({'file': file}) - text += \"Shields:\\n\" - dir = DirAccess.open(\"res://scenes/shields\") - files = dir.get_files() - for file in files: - text += \"{file}\\n\".format({'file': file}) - text += \"Weapons:\\n\" - dir = DirAccess.open(\"res://scenes/weapons/presets\") - files = dir.get_files() - for file in files: - text += \"{file}\\n\".format({'file': file}) - items.text = text - fetch_modules() - - -func fetch_modules(): - var dir = DirAccess.open(\"res://items\") - var files = dir.get_files() - var text = \"Items:\\n\" - for file in files: - text += \"{file}\\n\".format({'file': file}) - text += \"Verified Hulls:\\n\" - dir = DirAccess.open(\"res://scenes/hulls\") - var modules = dir.get_files() - files = check_for_item(modules) - for file in files: - text += \"{file}\\n\".format({'file': file}) - text += \"Verified Engines:\\n\" - dir = DirAccess.open(\"res://scenes/engines\") - modules = dir.get_files() - files = check_for_item(modules) - for file in files: - text += \"{file}\\n\".format({'file': file}) - text += \"Verified Shields:\\n\" - dir = DirAccess.open(\"res://scenes/shields\") - modules = dir.get_files() - files = check_for_item(modules) - for file in files: - text += \"{file}\\n\".format({'file': file}) - text += \"Verified Weapons:\\n\" - dir = DirAccess.open(\"res://scenes/weapons/presets\") - modules = dir.get_files() - files = check_for_item(modules) - for file in files: - text += \"{file}\\n\".format({'file': file}) - verified.text = text - -func check_for_item(modules : Array[String]) -> Array[String]: - var returnable : Array[String] = [] - for module in modules: - var itm = module.split('.', true, 1)[0] - if Game.get_item(itm).name != Game.DEFAULT_ITEM.name: - returnable.append(itm) - return returnable - - -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(_delta): - if Input.is_action_just_released(\"pause\"): - get_tree().change_scene_to_file(\"res://scenes/MainMenu.tscn\") -" - -[sub_resource type="ShaderMaterial" id="ShaderMaterial_m15k8"] +[sub_resource type="ShaderMaterial" id="ShaderMaterial_ogy53"] resource_local_to_scene = true resource_name = "Shield Material" shader = ExtResource("2_15op6") @@ -100,25 +16,10 @@ shader_parameter/max_distance = 20.0 shader_parameter/bublic_size = 20.0 [node name="Debug" type="Node2D"] -script = SubResource("GDScript_6w8qb") - -[node name="Items" type="Label" parent="."] -offset_right = 640.0 -offset_bottom = 720.0 -horizontal_alignment = 1 -vertical_alignment = 1 -autowrap_mode = 2 - -[node name="Verified Items" type="Label" parent="."] -offset_left = 640.0 -offset_right = 1280.0 -offset_bottom = 720.0 -horizontal_alignment = 1 -vertical_alignment = 1 -autowrap_mode = 2 +script = ExtResource("1_du5mm") [node name="PlayerShip" parent="." instance=ExtResource("1_b1l1c")] -material = SubResource("ShaderMaterial_m15k8") +material = SubResource("ShaderMaterial_ogy53") position = Vector2(608, 352) scale = Vector2(0.5, 0.5) diff --git a/scripts/Ship/hull.gd b/scripts/Ship/hull.gd index 7117f3b..37428db 100644 --- a/scripts/Ship/hull.gd +++ b/scripts/Ship/hull.gd @@ -28,6 +28,9 @@ class_name Hull else: ship.destroyed.emit() +## Length of linear_velocity vector +var scalar_velocity: float = 0.0 + ## Adds amount to ammunition, returns true if it was successful func add_ammunition(which: String, value: float) -> bool: if ammunition[which] + value < 0: @@ -41,3 +44,4 @@ func add_ammunition(which: String, value: float) -> bool: func _physics_process(_delta): ship.position = position ship.rotation = rotation + scalar_velocity = linear_velocity.length() diff --git a/scripts/Ship/player_camera.gd b/scripts/Ship/player_camera.gd new file mode 100644 index 0000000..ac43db3 --- /dev/null +++ b/scripts/Ship/player_camera.gd @@ -0,0 +1,16 @@ +extends Camera2D + +## Desired value of zoom +@export var normal_zoom: float = 0.4 +## Value of hull's scalar_velocity where zoom will be equal to normal_zoom +@export var normal_zoom_velocity: float = 200.0 +## Maximum value of zoom +@export var max_zoom: float = 0.5 +## Shortcut to get_parent() +@onready var ship: PlayerShip = get_parent() + +func _process(_delta): + var scalar_velocity = ship.hull.scalar_velocity + zoom = Vector2(1, 1) * normal_zoom * (normal_zoom_velocity / (scalar_velocity + 1)) + if zoom.x > max_zoom: + zoom = Vector2(1, 1) * max_zoom diff --git a/scripts/Ship/player_input_controller.gd b/scripts/Ship/player_input_controller.gd index 68e434e..9fc452b 100644 --- a/scripts/Ship/player_input_controller.gd +++ b/scripts/Ship/player_input_controller.gd @@ -12,3 +12,4 @@ func _physics_process(_delta) -> void: weapon.shoot_request = Input.get_action_strength("shootprimary") "secondary": weapon.shoot_request = Input.get_action_strength("shootsecondary") + diff --git a/scripts/Ship/player_ship.gd b/scripts/Ship/player_ship.gd new file mode 100644 index 0000000..6fb237d --- /dev/null +++ b/scripts/Ship/player_ship.gd @@ -0,0 +1,9 @@ +extends Ship + +class_name PlayerShip + +## Player camera reference +@onready var camera = $Camera + +## Currency variable +var money: float = 1000.0 diff --git a/scripts/Ship/ship.gd b/scripts/Ship/ship.gd index 2a8673e..23bd040 100644 --- a/scripts/Ship/ship.gd +++ b/scripts/Ship/ship.gd @@ -2,8 +2,6 @@ extends Node2D class_name Ship -# TODO: add weapons - ## Emits when hull hp reaches zero. signal destroyed @@ -15,24 +13,28 @@ signal destroyed @onready var shield: Shield = $Shield ## Reference to weapons node @onready var weapons: Node2D = $Weapons +## Node beginning position +@onready var spawn_position: Vector2 = global_position ## Faction which this ship belongs to var faction : Game.Faction = Game.Faction.Player func _ready() -> void: - hull.global_position += global_position + hull.global_position = global_position destroyed.connect(destroy) ## Reset all required variables func destroy() -> void: hull.hp = hull.max_hp + hull.global_position = spawn_position shield.capacity = shield.max_capacity ## Swaps old hull with the new one func change_hull(new_hull_id: String) -> void: + var hull_holder: Node = hull.get_parent() var new_hull: Hull = Game.get_module(new_hull_id, 'hull').instantiate() var old_hull: Hull = hull - add_child(new_hull) + hull_holder.add_child(new_hull) hull = new_hull hull.hp = old_hull.hp get_tree().create_timer(0.05).timeout.connect(old_hull.queue_free) diff --git a/scripts/Space.gd b/scripts/Spaceold.gd similarity index 100% rename from scripts/Space.gd rename to scripts/Spaceold.gd diff --git a/scripts/misc/ProjectilesContainer.gd b/scripts/misc/ProjectilesContainer.gd index c530b9f..50c493e 100644 --- a/scripts/misc/ProjectilesContainer.gd +++ b/scripts/misc/ProjectilesContainer.gd @@ -1,4 +1,4 @@ -extends Node2D +extends Node class_name ProjectileContainer @@ -6,3 +6,6 @@ static var instance : ProjectileContainer func _ready(): instance = self + +func _exit_tree(): + instance = null diff --git a/scripts/misc/StarsGeneration.gd b/scripts/misc/StarsGeneration.gd deleted file mode 100644 index ba96756..0000000 --- a/scripts/misc/StarsGeneration.gd +++ /dev/null @@ -1,18 +0,0 @@ -extends Node - -@export var star : PackedScene -@export var stars_amount = 1000 -@export var compress_space_amount = 0.5 - -func _ready(): - var compress_multiplier = 1-compress_space_amount - - var map_width_halved = get_tree().current_scene.map_width * compress_multiplier - var map_height_halved = get_tree().current_scene.map_height * compress_multiplier - for i in range(stars_amount): - var star_inst = star.instantiate() - var x = randi_range(-map_width_halved, map_width_halved) - var y = randi_range(-map_height_halved, map_height_halved) - var distance = randi_range(0,get_child_count()-1) - get_children()[distance].add_child(star_inst) - star_inst.position = Vector2(x, y) diff --git a/scripts/misc/stars_generator.gd b/scripts/misc/stars_generator.gd new file mode 100644 index 0000000..3afb801 --- /dev/null +++ b/scripts/misc/stars_generator.gd @@ -0,0 +1,28 @@ +extends Node + +# TODO: this just doesn't work + +## Star scene +@export var star : PackedScene +## Affects how many stars will be generated and places on whole system +@export var stars_amount = 1000 +## Affects how much space on borders of the star system will be empty +@export_range(0, 1) var compress_space_amount = 0.1 +## Shortcut to get_parent() +@onready var star_system: StarSystem = get_parent() + +func _ready(): + var compress_multiplier = 1.0 - compress_space_amount + var parallax_layers = get_children() + var width_halved = star_system.width / 2 + var height_halved = star_system.height / 2 + for i in range(stars_amount): + var star_inst = star.instantiate() + var x = randi_range(-width_halved, width_halved) * compress_multiplier + var y = randi_range(-height_halved, height_halved) * compress_multiplier + # Decides parallax layer which star belongs to + var distance = randi_range(0, get_child_count() - 1) + parallax_layers[distance].add_child(star_inst) + var position = Vector2(x, y) + star_inst.position = position + print(position) diff --git a/scripts/nebula.gd b/scripts/nebula.gd index 29818b2..b8a761b 100644 --- a/scripts/nebula.gd +++ b/scripts/nebula.gd @@ -1,11 +1,15 @@ extends ColorRect -@onready var ship = get_tree().current_scene.get_node("MainShip") -@onready var space = get_tree().current_scene +## Node which will change offset. If null, offset is constant +@export var tracked_node: Node2D +## Shortcut to get_parent() +@onready var star_system = get_parent() +## Color of nebula. +@export var color_background: Color + +func _ready(): + material.set_shader_parameter("nebula_color", color_background) func _physics_process(_delta): - material.set_shader_parameter("offset",ship.global_position) - - -func _on_space_updated_colors(): - material.set_shader_parameter("nebula_color",space.color_background) + if tracked_node != null: + material.set_shader_parameter("offset", tracked_node.global_position) diff --git a/scripts/objects/Star.gd b/scripts/objects/Star.gd index 534e08d..f8ba442 100644 --- a/scripts/objects/Star.gd +++ b/scripts/objects/Star.gd @@ -8,3 +8,4 @@ func _ready(): speed_scale = randf_range(0, 2) var Colors = [Color.LIGHT_BLUE, Color.WHITE, Color.LIGHT_GOLDENROD, Color.YELLOW, Color.ORANGE, Color.ORANGE_RED, Color.RED] modulate = Colors[randi_range(0, 6)] + print("Star born at",global_position) diff --git a/scripts/star_system.gd b/scripts/star_system.gd new file mode 100644 index 0000000..f90f3ba --- /dev/null +++ b/scripts/star_system.gd @@ -0,0 +1,17 @@ +extends Node + +class_name StarSystem + +@export var width: int = 8192 +@export var height: int = 8192 + +var player_ship: PlayerShip + +func _ready(): + player_ship = get_node_or_null("PlayerShip") + + if player_ship != null: + player_ship.camera.limit_left = -width / 2 + player_ship.camera.limit_right = -width / 2 + player_ship.camera.limit_top = -height / 2 + player_ship.camera.limit_bottom = -height / 2 diff --git a/shaders/materials/nebula_shader.tres b/shaders/materials/nebula_shader.tres new file mode 100644 index 0000000..895cd65 --- /dev/null +++ b/shaders/materials/nebula_shader.tres @@ -0,0 +1,16 @@ +[gd_resource type="ShaderMaterial" load_steps=4 format=3 uid="uid://bawhivm5cr2w"] + +[ext_resource type="Shader" path="res://shaders/nebula.gdshader" id="1_gp8q1"] + +[sub_resource type="FastNoiseLite" id="FastNoiseLite_n2xxx"] + +[sub_resource type="NoiseTexture2D" id="NoiseTexture2D_lcrcg"] +seamless = true +noise = SubResource("FastNoiseLite_n2xxx") + +[resource] +shader = ExtResource("1_gp8q1") +shader_parameter/divisor = 10000.0 +shader_parameter/offset = Vector2(0, 0) +shader_parameter/nebula_color = Color(0.0666667, 0.0666667, 0.188235, 1) +shader_parameter/noise_texture = SubResource("NoiseTexture2D_lcrcg")