Open1kJ/Field/field.gd

54 lines
1.3 KiB
GDScript3
Raw Normal View History

2024-02-24 22:25:53 -06:00
extends Node2D
var width: int
var height: int
var tiles: Array = []
var x = 1
var homes
var next_home
func _init():
var bytes = PakAssetLoader.load_file("user://data/fields.pak/field_practice.fld")
create_from_buffer(bytes)
# Called when the node enters the scene tree for the first time.
func _ready():
homes = tiles.filter(is_home)
$Player.position = homes[0].position
next_home = homes[1].position
homes = [homes[0], homes[1], homes[3], homes[2]]
pass
func is_home(tile):
return tile.id == Enums.TileID.TileHome
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
if $Player.position != next_home:
$Player.position = $Player.position.move_toward(next_home, 4)
else:
x += 1
next_home = homes[x % 4].position
pass
func create_from_buffer(bytes: PackedByteArray):
var num_tiles = bytes.size() / 8
# all boards are square
height = int(sqrt(num_tiles))
width = int(sqrt(num_tiles))
var j = 0;
for i in range(0, num_tiles):
var tile = load("res://Field/tile.tscn").instantiate()
tile.id = bytes.decode_u32(i * 8)
var exit_bits = bytes.decode_u8((i * 8) + 4)
add_child.call_deferred(tile)
tile.position = Vector2(((i % width) * 158) + 64, (j * 158) + 64)
tiles.append(tile)
if i % height == height - 1:
j += 1
#print(bytes)
pass