Use Singleton for map and player config

This commit is contained in:
DeathByDenim 2023-05-22 13:41:23 -04:00
parent 92726a3290
commit 07e9aefbaa
Signed by: DeathByDenim
GPG Key ID: 4A475283D925365B
11 changed files with 58 additions and 35 deletions

View File

@ -4,18 +4,18 @@ var base_positions: Array[Vector2i] = []
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready(): func _ready():
$Options.reset_to_defaults() MapConfig.reset_to_defaults()
func load_map(file_name: String): func load_map(file_name: String):
$Options.num_errors = 0 MapConfig.num_errors = 0
$Options.reset_to_defaults() MapConfig.reset_to_defaults()
parse_map_file(file_name) parse_map_file(file_name)
apply_options() apply_options()
prints("Loaded", $Options.mapname, "by", $Options.mapauthor) prints("Loaded", MapConfig.mapname, "by", MapConfig.mapauthor)
if $Options.num_errors > 0: if MapConfig.num_errors > 0:
push_error("Map contains ", $Options.num_errors, " errors") push_error("Map contains ", MapConfig.num_errors, " errors")
func parse_map_file(file_name): func parse_map_file(file_name):
@ -47,7 +47,7 @@ func parse_map_file(file_name):
line_number += 1 line_number += 1
if multi_line_mode_option: if multi_line_mode_option:
if line.contains(multi_line_terminator): 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 = "" multi_line_mode_option = ""
else: else:
multi_line_content += line 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_mode_option = option[0].strip_edges().to_lower()
multi_line_terminator = option[1].split("\\multiline:")[1].strip_edges() multi_line_terminator = option[1].split("\\multiline:")[1].strip_edges()
continue 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 != "": if multi_line_mode_option != "":
print("EOF reached before end of multiline") print("EOF reached before end of multiline")
if $Options.mapdata.length() > 0 and $Options.mapwidth > 0 and $Options.mapheight > 0: if MapConfig.mapdata.length() > 0 and MapConfig.mapwidth > 0 and MapConfig.mapheight > 0:
for iy in range($Options.mapheight): for iy in range(MapConfig.mapheight):
for ix in range($Options.mapwidth): for ix in range(MapConfig.mapwidth):
var letter_code = $Options.mapdata[iy * $Options.mapwidth + ix] var letter_code = MapConfig.mapdata[iy * MapConfig.mapwidth + ix]
if tile_dictionary.has(letter_code): if tile_dictionary.has(letter_code):
set_cell(0, Vector2i(ix, iy), 0, tile_dictionary[letter_code]) set_cell(0, Vector2i(ix, iy), 0, tile_dictionary[letter_code])
if "_0123456789".contains(letter_code): if "_0123456789".contains(letter_code):
base_positions.push_back(Vector2i(ix, iy)) base_positions.push_back(Vector2i(ix, iy))
if $Options.edgewrap: if MapConfig.edgewrap:
if ix < $Options.mapwidth / 2: if ix < MapConfig.mapwidth / 2:
base_positions[-1].x += $Options.mapwidth base_positions[-1].x += MapConfig.mapwidth
if iy < $Options.mapheight / 2: if iy < MapConfig.mapheight / 2:
base_positions[-1].y += $Options.mapheight base_positions[-1].y += MapConfig.mapheight
if $Options.edgewrap: if MapConfig.edgewrap:
set_cell(0, Vector2i(ix + $Options.mapwidth, iy), 0, tile_dictionary[letter_code]) set_cell(0, Vector2i(ix + MapConfig.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 + MapConfig.mapwidth, iy + MapConfig.mapheight), 0, tile_dictionary[letter_code])
set_cell(0, Vector2i(ix, iy + $Options.mapheight), 0, tile_dictionary[letter_code]) set_cell(0, Vector2i(ix, iy + MapConfig.mapheight), 0, tile_dictionary[letter_code])
else: else:
print("Map data or dimensions missing") print("Map data or dimensions missing")

View File

@ -2,6 +2,7 @@ extends MultiplayerSynchronizer
@export var rotate := 0.0 @export var rotate := 0.0
@export var thrust := false @export var thrust := false
@export var shoot := false
const ROTATION = 0.05 const ROTATION = 0.05
@ -18,3 +19,5 @@ func _process(delta):
if Input.is_action_pressed("right"): if Input.is_action_pressed("right"):
rotate += ROTATION rotate += ROTATION
thrust = Input.is_action_pressed("thrust") thrust = Input.is_action_pressed("thrust")
shoot = Input.is_action_pressed("shoot")

View File

@ -28,7 +28,7 @@ func _ready():
# EDIT: Left the client simulate player movement too to compesate network latency. # EDIT: Left the client simulate player movement too to compesate network latency.
# set_physics_process(multiplayer.is_server()) # 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(): func shoot():
# "Muzzle" is a Marker2D placed at the barrel of the gun. # "Muzzle" is a Marker2D placed at the barrel of the gun.
@ -44,8 +44,10 @@ func _physics_process(delta):
if input.thrust: if input.thrust:
velocity += Vector2(thrust, 0).rotated(rotation) velocity += Vector2(thrust, 0).rotated(rotation)
if input.shoot:
shoot()
var collision = move_and_collide(velocity * delta) var collision = move_and_collide(velocity * delta)
if collision: if collision:
velocity = (1 - $"../../Map/Options".playerwallbouncebrakefactor) * velocity.bounce(collision.get_normal()) velocity = (1 - MapConfig.playerwallbouncebrakefactor) * velocity.bounce(collision.get_normal())

View File

@ -22,6 +22,9 @@ properties/0/sync = true
properties/1/path = NodePath(".:thrust") properties/1/path = NodePath(".:thrust")
properties/1/spawn = false properties/1/spawn = false
properties/1/sync = true properties/1/sync = true
properties/2/path = NodePath(".:shoot")
properties/2/spawn = false
properties/2/sync = true
[node name="Player ship" type="CharacterBody2D"] [node name="Player ship" type="CharacterBody2D"]
script = ExtResource("1_tcnl2") script = ExtResource("1_tcnl2")

View File

@ -45,8 +45,8 @@ func _process(delta):
func _on_map_boundaries_body_exited(body): func _on_map_boundaries_body_exited(body):
var width = $Map/Options.mapwidth var width = MapConfig.mapwidth
var height = $Map/Options.mapheight var height = MapConfig.mapheight
if body.position.x < $"Map boundaries".position.x - 32*width: if body.position.x < $"Map boundaries".position.x - 32*width:
body.position.x += 64 * width body.position.x += 64 * width

View File

@ -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="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="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/Map.gd" id="3_1prtd"]
[ext_resource type="Script" path="res://World/Options.gd" id="4_36ml1"]
[sub_resource type="TileSetAtlasSource" id="TileSetAtlasSource_b2nj3"] [sub_resource type="TileSetAtlasSource" id="TileSetAtlasSource_b2nj3"]
texture = ExtResource("2_covyd") texture = ExtResource("2_covyd")
@ -172,9 +171,6 @@ cell_quadrant_size = 64
format = 2 format = 2
script = ExtResource("3_1prtd") script = ExtResource("3_1prtd")
[node name="Options" type="Node" parent="Map"]
script = ExtResource("4_36ml1")
[node name="Map boundaries" type="Area2D" parent="."] [node name="Map boundaries" type="Area2D" parent="."]
[node name="CollisionShape2D" type="CollisionShape2D" parent="Map boundaries"] [node name="CollisionShape2D" type="CollisionShape2D" parent="Map boundaries"]

View File

@ -3,7 +3,7 @@ extends CharacterBody2D
# Get the gravity from the project settings to be synced with RigidBody nodes. # Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity") var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var speed = 750 var speed = 750
var life_time = 1.0 @onready var life_time: float = MapConfig.shotlife
func start(_position, _direction): func start(_position, _direction):
rotation = _direction rotation = _direction

View File

@ -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"] [ext_resource type="Script" path="res://World/bullet.gd" id="1_j3y75"]

View File

@ -263,7 +263,10 @@ var maxroundtime: int
var roundstoplay: int var roundstoplay: int
var maxpausetime: int var maxpausetime: int
func reset_to_defaults(): func _ready() -> void:
reset_to_defaults()
func reset_to_defaults() -> void:
gravity = -0.14 gravity = -0.14
shipmass = 20.0 shipmass = 20.0
ballmass = 50.0 ballmass = 50.0
@ -525,7 +528,7 @@ func reset_to_defaults():
roundstoplay = 0 roundstoplay = 0
maxpausetime = 3600 maxpausetime = 3600
func set_option(option: String, value: String): func set_option(option: String, value: String) -> void:
option = option.to_lower() option = option.to_lower()
match option: match option:
"gravity": "gravity":

11
player_config.gd Normal file
View File

@ -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

View File

@ -15,6 +15,11 @@ run/main_scene="res://Lobby/Lobby.tscn"
config/features=PackedStringArray("4.0", "Mobile") config/features=PackedStringArray("4.0", "Mobile")
config/icon="res://icon.svg" config/icon="res://icon.svg"
[autoload]
PlayerConfig="*res://player_config.gd"
MapConfig="*res://map_config.gd"
[input] [input]
thrust={ thrust={