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.
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")

View File

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

View File

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

View File

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

View File

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

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="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"]

View File

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

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"]

View File

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

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/icon="res://icon.svg"
[autoload]
PlayerConfig="*res://player_config.gd"
MapConfig="*res://map_config.gd"
[input]
thrust={