Open1kJ/Field/field.gd

54 lines
1.4 KiB
GDScript

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():
$BGSprite.texture = PakAssetLoader.load_file("user://data/fields.pak/practice_l.dat")
homes = tiles.filter(is_home)
$Unit.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):
var move_speed = delta * 60.0
if $Unit.position != next_home:
$Unit.position = $Unit.position.move_toward(next_home, move_speed * 8)
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