Add all map options
This commit is contained in:
parent
bf28e6ee01
commit
9d5dbadc64
|
@ -0,0 +1,101 @@
|
|||
extends TileMap
|
||||
|
||||
var base_positions: Array[Vector2i] = []
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
$Options.reset_to_defaults()
|
||||
|
||||
|
||||
|
||||
func load_map(file_name: String):
|
||||
$Options.num_errors = 0
|
||||
$Options.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")
|
||||
|
||||
|
||||
func parse_map_file(file_name):
|
||||
# Remove current map if any
|
||||
clear()
|
||||
|
||||
# Build a dictionary that translates letter codes from
|
||||
# the map format to tiles
|
||||
var tile_dictionary = {}
|
||||
var atlas_source = tile_set.get_source(0)
|
||||
for i in range(atlas_source.get_tiles_count()):
|
||||
var tile_id = atlas_source.get_tile_id(i)
|
||||
var tile_data = atlas_source.get_tile_data(tile_id, 0)
|
||||
var letter_code = tile_data.get_custom_data("Letter code")
|
||||
for letter in letter_code:
|
||||
tile_dictionary[letter] = tile_id
|
||||
|
||||
# Load the map from file
|
||||
var mapfile = FileAccess.open(file_name, FileAccess.READ)
|
||||
if not mapfile:
|
||||
print("Couldn't open map file")
|
||||
return
|
||||
var multi_line_mode_option = ""
|
||||
var multi_line_terminator = ""
|
||||
var multi_line_content = ""
|
||||
var line_number = 0
|
||||
while not mapfile.eof_reached():
|
||||
var line = mapfile.get_line()
|
||||
line_number += 1
|
||||
if multi_line_mode_option:
|
||||
if line.contains(multi_line_terminator):
|
||||
$Options.set_option(multi_line_mode_option, multi_line_content)
|
||||
multi_line_mode_option = ""
|
||||
else:
|
||||
multi_line_content += line
|
||||
else:
|
||||
# Remove comments
|
||||
var comment_pos = line.find('#')
|
||||
if comment_pos >= 0:
|
||||
line = line.left(comment_pos)
|
||||
# Get rid of extra whitespace
|
||||
line = line.strip_edges()
|
||||
# If what is left over is an empty line, skip it
|
||||
if line.length() == 0:
|
||||
continue
|
||||
var option = line.split(':', true, 1)
|
||||
if option.size() != 2:
|
||||
print("Invalid data on line ", line_number)
|
||||
continue
|
||||
# Multiline values expand over many lindes until a specified keyword is reached
|
||||
if option[1].contains("\\multiline:"):
|
||||
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())
|
||||
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 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])
|
||||
|
||||
else:
|
||||
print("Map data or dimensions missing")
|
||||
|
||||
func apply_options():
|
||||
# $"Map boundaries/CollisionShape2D".shape.size = 64 * Vector2i(width, height)
|
||||
# $"Map boundaries".position = 64 * Vector2i(width, height)
|
||||
pass
|
File diff suppressed because it is too large
Load Diff
|
@ -8,7 +8,6 @@ var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
|
|||
var Bullet = preload("res://World/bullet.tscn")
|
||||
|
||||
var thrust = 6.0
|
||||
var collision_damping = 0.1
|
||||
|
||||
# Set by the authority, synchronized on spawn.
|
||||
@export var player := 1 :
|
||||
|
@ -47,5 +46,5 @@ func _physics_process(delta):
|
|||
|
||||
var collision = move_and_collide(velocity * delta)
|
||||
if collision:
|
||||
velocity = (1 - collision_damping) * velocity.bounce(collision.get_normal())
|
||||
velocity = (1 - $"../../Map/Options".playerwallbouncebrakefactor) * velocity.bounce(collision.get_normal())
|
||||
|
||||
|
|
|
@ -1,12 +1,8 @@
|
|||
extends Node2D
|
||||
|
||||
var options = {}
|
||||
var base_positions: Array[Vector2i]
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
default_options()
|
||||
load_map("res://Maps/default.xp")
|
||||
$Map.load_map("res://Maps/default.xp")
|
||||
# $"Player ship".position = Vector2(500., 500.)
|
||||
if not multiplayer.is_server():
|
||||
return
|
||||
|
@ -32,7 +28,7 @@ func add_player(id: int):
|
|||
var player_ship = preload("res://World/PlayerShip.tscn").instantiate()
|
||||
# Set player id.
|
||||
player_ship.player = id
|
||||
player_ship.position = 64*base_positions[randi() % base_positions.size()] + Vector2i(32, 32)
|
||||
player_ship.position = 64*$Map.base_positions[randi() % $Map.base_positions.size()] + Vector2i(32, 32)
|
||||
player_ship.rotation_degrees = -90
|
||||
player_ship.name = str(id)
|
||||
$Players.add_child(player_ship, true)
|
||||
|
@ -43,111 +39,14 @@ func del_player(id: int):
|
|||
return
|
||||
$Players.get_node(str(id)).queue_free()
|
||||
|
||||
func default_options():
|
||||
options["gravity"] = 100
|
||||
options["mapheight"] = 100
|
||||
|
||||
func load_map(file_name):
|
||||
parse_map_file(file_name)
|
||||
apply_options()
|
||||
|
||||
func parse_map_file(file_name):
|
||||
# Remove current map if any
|
||||
$TileMap.clear()
|
||||
|
||||
# Build a dictionary that translates letter codes from
|
||||
# the map format to tiles
|
||||
var tile_dictionary = {}
|
||||
var atlas_source = $TileMap.tile_set.get_source(0)
|
||||
for i in range(atlas_source.get_tiles_count()):
|
||||
var tile_id = atlas_source.get_tile_id(i)
|
||||
var tile_data = atlas_source.get_tile_data(tile_id, 0)
|
||||
var letter_code = tile_data.get_custom_data("Letter code")
|
||||
for letter in letter_code:
|
||||
tile_dictionary[letter] = tile_id
|
||||
|
||||
# Load the map from file
|
||||
var mapfile = FileAccess.open(file_name, FileAccess.READ)
|
||||
if not mapfile:
|
||||
print("Couldn't open map file")
|
||||
return
|
||||
var multi_line_mode_option = ""
|
||||
var multi_line_terminator = ""
|
||||
var multi_line_content = ""
|
||||
var line_number = 0
|
||||
while not mapfile.eof_reached():
|
||||
var line = mapfile.get_line()
|
||||
line_number += 1
|
||||
if multi_line_mode_option:
|
||||
if line.contains(multi_line_terminator):
|
||||
options[multi_line_mode_option] = multi_line_content
|
||||
multi_line_mode_option = ""
|
||||
else:
|
||||
multi_line_content += line
|
||||
else:
|
||||
# Remove comments
|
||||
var comment_pos = line.find('#')
|
||||
if comment_pos >= 0:
|
||||
line = line.left(comment_pos)
|
||||
# Get rid of extra whitespace
|
||||
line = line.strip_edges()
|
||||
# If what is left over is an empty line, skip it
|
||||
if line.length() == 0:
|
||||
continue
|
||||
var option = line.split(':', true, 1)
|
||||
if option.size() != 2:
|
||||
print("Invalid data on line ", line_number)
|
||||
continue
|
||||
# Multiline values expand over many lindes until a specified keyword is reached
|
||||
if option[1].contains("\\multiline:"):
|
||||
multi_line_mode_option = option[0].strip_edges().to_lower()
|
||||
multi_line_terminator = option[1].split("\\multiline:")[1].strip_edges()
|
||||
continue
|
||||
options[option[0].strip_edges().to_lower()] = option[1].strip_edges()
|
||||
if multi_line_mode_option != "":
|
||||
print("EOF reached before end of multiline")
|
||||
|
||||
var edge_wrap = false
|
||||
if options.has("edgewrap"):
|
||||
edge_wrap = (options["edgewrap"] == "yes")
|
||||
|
||||
if options.has("mapdata") and options.has("mapwidth") and options.has("mapheight"):
|
||||
var width = int(options["mapwidth"])
|
||||
var height = int(options["mapheight"])
|
||||
var data = options["mapdata"]
|
||||
for iy in range(height):
|
||||
for ix in range(width):
|
||||
var letter_code = data[iy * width + ix]
|
||||
if tile_dictionary.has(letter_code):
|
||||
$TileMap.set_cell(0, Vector2i(ix, iy), 0, tile_dictionary[letter_code])
|
||||
if "_0123456789".contains(letter_code):
|
||||
base_positions.push_back(Vector2i(ix, iy))
|
||||
if edge_wrap:
|
||||
if ix < width / 2:
|
||||
base_positions[-1].x += width
|
||||
if iy < height / 2:
|
||||
base_positions[-1].y += height
|
||||
if edge_wrap:
|
||||
$TileMap.set_cell(0, Vector2i(ix + width, iy), 0, tile_dictionary[letter_code])
|
||||
$TileMap.set_cell(0, Vector2i(ix + width, iy + height), 0, tile_dictionary[letter_code])
|
||||
$TileMap.set_cell(0, Vector2i(ix, iy + height), 0, tile_dictionary[letter_code])
|
||||
|
||||
else:
|
||||
print("Map data or dimensions missing")
|
||||
|
||||
func apply_options():
|
||||
var width = int(options["mapwidth"])
|
||||
var height = int(options["mapheight"])
|
||||
$"Map boundaries/CollisionShape2D".shape.size = 64 * Vector2i(width, height)
|
||||
$"Map boundaries".position = 64 * Vector2i(width, height)
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta):
|
||||
pass
|
||||
|
||||
func _on_map_boundaries_body_exited(body):
|
||||
var width = int(options["mapwidth"])
|
||||
var height = int(options["mapheight"])
|
||||
|
||||
var width = $Map/Options.mapwidth
|
||||
var height = $Map/Options.mapheight
|
||||
|
||||
if body.position.x < $"Map boundaries".position.x - 32*width:
|
||||
body.position.x += 64 * width
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
[gd_scene load_steps=6 format=3 uid="uid://ddr7q5f0xrfsm"]
|
||||
[gd_scene load_steps=8 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")
|
||||
|
@ -164,10 +166,14 @@ size = Vector2(6400, 6400)
|
|||
scale = Vector2(0.5, 0.5)
|
||||
script = ExtResource("1_ashcc")
|
||||
|
||||
[node name="TileMap" type="TileMap" parent="."]
|
||||
[node name="Map" type="TileMap" parent="."]
|
||||
tile_set = SubResource("TileSet_8m2mo")
|
||||
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="."]
|
||||
|
||||
|
|
Loading…
Reference in New Issue