From 07e9aefbaa9e31e8a3fa83699d765d13ae99a268 Mon Sep 17 00:00:00 2001 From: DeathByDenim Date: Mon, 22 May 2023 13:41:23 -0400 Subject: [PATCH] Use Singleton for map and player config --- World/Map.gd | 42 +++++++++++++++---------------- World/PlayerInput.gd | 3 +++ World/PlayerShip.gd | 8 +++--- World/PlayerShip.tscn | 3 +++ World/PlayingField.gd | 4 +-- World/PlayingField.tscn | 6 +---- World/bullet.gd | 2 +- World/bullet.tscn | 2 +- World/Options.gd => map_config.gd | 7 ++++-- player_config.gd | 11 ++++++++ project.godot | 5 ++++ 11 files changed, 58 insertions(+), 35 deletions(-) rename World/Options.gd => map_config.gd (99%) create mode 100644 player_config.gd diff --git a/World/Map.gd b/World/Map.gd index 2d8dc6d..1cd1e48 100644 --- a/World/Map.gd +++ b/World/Map.gd @@ -4,18 +4,18 @@ var base_positions: Array[Vector2i] = [] # Called when the node enters the scene tree for the first time. func _ready(): - $Options.reset_to_defaults() + MapConfig.reset_to_defaults() func load_map(file_name: String): - $Options.num_errors = 0 - $Options.reset_to_defaults() + MapConfig.num_errors = 0 + MapConfig.reset_to_defaults() parse_map_file(file_name) apply_options() - prints("Loaded", $Options.mapname, "by", $Options.mapauthor) - if $Options.num_errors > 0: - push_error("Map contains ", $Options.num_errors, " errors") + prints("Loaded", MapConfig.mapname, "by", MapConfig.mapauthor) + if MapConfig.num_errors > 0: + push_error("Map contains ", MapConfig.num_errors, " errors") func parse_map_file(file_name): @@ -47,7 +47,7 @@ func parse_map_file(file_name): line_number += 1 if multi_line_mode_option: if line.contains(multi_line_terminator): - $Options.set_option(multi_line_mode_option, multi_line_content) + MapConfig.set_option(multi_line_mode_option, multi_line_content) multi_line_mode_option = "" else: multi_line_content += line @@ -70,27 +70,27 @@ func parse_map_file(file_name): multi_line_mode_option = option[0].strip_edges().to_lower() multi_line_terminator = option[1].split("\\multiline:")[1].strip_edges() continue - $Options.set_option(option[0].strip_edges(), option[1].strip_edges()) + MapConfig.set_option(option[0].strip_edges(), option[1].strip_edges()) if multi_line_mode_option != "": print("EOF reached before end of multiline") - if $Options.mapdata.length() > 0 and $Options.mapwidth > 0 and $Options.mapheight > 0: - for iy in range($Options.mapheight): - for ix in range($Options.mapwidth): - var letter_code = $Options.mapdata[iy * $Options.mapwidth + ix] + if MapConfig.mapdata.length() > 0 and MapConfig.mapwidth > 0 and MapConfig.mapheight > 0: + for iy in range(MapConfig.mapheight): + for ix in range(MapConfig.mapwidth): + var letter_code = MapConfig.mapdata[iy * MapConfig.mapwidth + ix] if tile_dictionary.has(letter_code): set_cell(0, Vector2i(ix, iy), 0, tile_dictionary[letter_code]) if "_0123456789".contains(letter_code): base_positions.push_back(Vector2i(ix, iy)) - if $Options.edgewrap: - if ix < $Options.mapwidth / 2: - base_positions[-1].x += $Options.mapwidth - if iy < $Options.mapheight / 2: - base_positions[-1].y += $Options.mapheight - if $Options.edgewrap: - set_cell(0, Vector2i(ix + $Options.mapwidth, iy), 0, tile_dictionary[letter_code]) - set_cell(0, Vector2i(ix + $Options.mapwidth, iy + $Options.mapheight), 0, tile_dictionary[letter_code]) - set_cell(0, Vector2i(ix, iy + $Options.mapheight), 0, tile_dictionary[letter_code]) + if MapConfig.edgewrap: + if ix < MapConfig.mapwidth / 2: + base_positions[-1].x += MapConfig.mapwidth + if iy < MapConfig.mapheight / 2: + base_positions[-1].y += MapConfig.mapheight + if MapConfig.edgewrap: + set_cell(0, Vector2i(ix + MapConfig.mapwidth, iy), 0, tile_dictionary[letter_code]) + set_cell(0, Vector2i(ix + MapConfig.mapwidth, iy + MapConfig.mapheight), 0, tile_dictionary[letter_code]) + set_cell(0, Vector2i(ix, iy + MapConfig.mapheight), 0, tile_dictionary[letter_code]) else: print("Map data or dimensions missing") diff --git a/World/PlayerInput.gd b/World/PlayerInput.gd index 65efbf5..c25a915 100644 --- a/World/PlayerInput.gd +++ b/World/PlayerInput.gd @@ -2,6 +2,7 @@ extends MultiplayerSynchronizer @export var rotate := 0.0 @export var thrust := false +@export var shoot := false const ROTATION = 0.05 @@ -18,3 +19,5 @@ func _process(delta): if Input.is_action_pressed("right"): rotate += ROTATION thrust = Input.is_action_pressed("thrust") + shoot = Input.is_action_pressed("shoot") + diff --git a/World/PlayerShip.gd b/World/PlayerShip.gd index 9360ac7..0bf7fb8 100644 --- a/World/PlayerShip.gd +++ b/World/PlayerShip.gd @@ -28,7 +28,7 @@ func _ready(): # EDIT: Left the client simulate player movement too to compesate network latency. # set_physics_process(multiplayer.is_server()) - gravity = 1000*$"../../Map/Options".gravity * Vector2.LEFT.rotated(deg_to_rad($"../../Map/Options".gravityangle)) + gravity = 1000*MapConfig.gravity * Vector2.LEFT.rotated(deg_to_rad(MapConfig.gravityangle)) func shoot(): # "Muzzle" is a Marker2D placed at the barrel of the gun. @@ -44,8 +44,10 @@ func _physics_process(delta): if input.thrust: velocity += Vector2(thrust, 0).rotated(rotation) + if input.shoot: + shoot() var collision = move_and_collide(velocity * delta) if collision: - velocity = (1 - $"../../Map/Options".playerwallbouncebrakefactor) * velocity.bounce(collision.get_normal()) - + velocity = (1 - MapConfig.playerwallbouncebrakefactor) * velocity.bounce(collision.get_normal()) + diff --git a/World/PlayerShip.tscn b/World/PlayerShip.tscn index 9d60df3..8248724 100644 --- a/World/PlayerShip.tscn +++ b/World/PlayerShip.tscn @@ -22,6 +22,9 @@ properties/0/sync = true properties/1/path = NodePath(".:thrust") properties/1/spawn = false properties/1/sync = true +properties/2/path = NodePath(".:shoot") +properties/2/spawn = false +properties/2/sync = true [node name="Player ship" type="CharacterBody2D"] script = ExtResource("1_tcnl2") diff --git a/World/PlayingField.gd b/World/PlayingField.gd index c7a0684..d23fbef 100644 --- a/World/PlayingField.gd +++ b/World/PlayingField.gd @@ -45,8 +45,8 @@ func _process(delta): func _on_map_boundaries_body_exited(body): - var width = $Map/Options.mapwidth - var height = $Map/Options.mapheight + var width = MapConfig.mapwidth + var height = MapConfig.mapheight if body.position.x < $"Map boundaries".position.x - 32*width: body.position.x += 64 * width diff --git a/World/PlayingField.tscn b/World/PlayingField.tscn index 3eb45e4..4fba8eb 100644 --- a/World/PlayingField.tscn +++ b/World/PlayingField.tscn @@ -1,9 +1,8 @@ -[gd_scene load_steps=8 format=3 uid="uid://ddr7q5f0xrfsm"] +[gd_scene load_steps=7 format=3 uid="uid://ddr7q5f0xrfsm"] [ext_resource type="Script" path="res://World/PlayingField.gd" id="1_ashcc"] [ext_resource type="Texture2D" uid="uid://d4hd1froa8gji" path="res://Images/tilemap.png" id="2_covyd"] [ext_resource type="Script" path="res://World/Map.gd" id="3_1prtd"] -[ext_resource type="Script" path="res://World/Options.gd" id="4_36ml1"] [sub_resource type="TileSetAtlasSource" id="TileSetAtlasSource_b2nj3"] texture = ExtResource("2_covyd") @@ -172,9 +171,6 @@ cell_quadrant_size = 64 format = 2 script = ExtResource("3_1prtd") -[node name="Options" type="Node" parent="Map"] -script = ExtResource("4_36ml1") - [node name="Map boundaries" type="Area2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="Map boundaries"] diff --git a/World/bullet.gd b/World/bullet.gd index 6fc3489..49cc545 100644 --- a/World/bullet.gd +++ b/World/bullet.gd @@ -3,7 +3,7 @@ extends CharacterBody2D # Get the gravity from the project settings to be synced with RigidBody nodes. var gravity = ProjectSettings.get_setting("physics/2d/default_gravity") var speed = 750 -var life_time = 1.0 +@onready var life_time: float = MapConfig.shotlife func start(_position, _direction): rotation = _direction diff --git a/World/bullet.tscn b/World/bullet.tscn index b8a2403..09226f0 100644 --- a/World/bullet.tscn +++ b/World/bullet.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=4 format=3] +[gd_scene load_steps=4 format=3 uid="uid://dc67u0fso8ivs"] [ext_resource type="Script" path="res://World/bullet.gd" id="1_j3y75"] diff --git a/World/Options.gd b/map_config.gd similarity index 99% rename from World/Options.gd rename to map_config.gd index 7e73608..e2a1d39 100644 --- a/World/Options.gd +++ b/map_config.gd @@ -263,7 +263,10 @@ var maxroundtime: int var roundstoplay: int var maxpausetime: int -func reset_to_defaults(): +func _ready() -> void: + reset_to_defaults() + +func reset_to_defaults() -> void: gravity = -0.14 shipmass = 20.0 ballmass = 50.0 @@ -525,7 +528,7 @@ func reset_to_defaults(): roundstoplay = 0 maxpausetime = 3600 -func set_option(option: String, value: String): +func set_option(option: String, value: String) -> void: option = option.to_lower() match option: "gravity": diff --git a/player_config.gd b/player_config.gd new file mode 100644 index 0000000..e08925d --- /dev/null +++ b/player_config.gd @@ -0,0 +1,11 @@ +extends Node + + +# 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 _process(delta): + pass diff --git a/project.godot b/project.godot index 19c7869..4dd60cc 100644 --- a/project.godot +++ b/project.godot @@ -15,6 +15,11 @@ run/main_scene="res://Lobby/Lobby.tscn" config/features=PackedStringArray("4.0", "Mobile") config/icon="res://icon.svg" +[autoload] + +PlayerConfig="*res://player_config.gd" +MapConfig="*res://map_config.gd" + [input] thrust={