impl field

This commit is contained in:
Candygoblen123 2024-02-24 23:25:53 -05:00
parent 91d06311db
commit 162fc09baa
No known key found for this signature in database
GPG Key ID: 577DA64EBEF10385
4 changed files with 104 additions and 0 deletions

53
Field/field.gd Normal file
View File

@ -0,0 +1,53 @@
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

11
Field/field.tscn Normal file
View File

@ -0,0 +1,11 @@
[gd_scene load_steps=4 format=3 uid="uid://qipfodwbk1gf"]
[ext_resource type="Script" path="res://Field/field.gd" id="1_6uvjw"]
[ext_resource type="PackedScene" uid="uid://sp3iv6cxuerm" path="res://player.tscn" id="2_oi7aa"]
[ext_resource type="Script" path="res://Player/player.gd" id="3_vsc2e"]
[node name="Field" type="Node2D"]
script = ExtResource("1_6uvjw")
[node name="Player" parent="." instance=ExtResource("2_oi7aa")]
script = ExtResource("3_vsc2e")

26
Field/tile.gd Normal file
View File

@ -0,0 +1,26 @@
extends Node2D
var id: Enums.TileID
var enter: Array
var exit: Array
# Called when the node enters the scene tree for the first time.
func _ready():
var tex = Enums.get_tile_texture(id)
if tex != "":
$Sprite2D.texture = PakAssetLoader.load_file("user://data/field.pak/" + tex)
$Base.texture = PakAssetLoader.load_file("user://data/field.pak/mass_base0.dat")
# if id == Enums.TileID.TileIce:
# var layer = Sprite2D.new()
# var snow = Enums.get_tile_texture(Enums.TileID.TileSnow)
# layer.texture = PakAssetLoader.load_file("user://data/field.pak/" + snow)
# layer.flip_v = true
# print($Sprite2D.z_index)
# $Sprite2D.add_child(layer)
# layer.show_behind_parent = true
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass

14
Field/tile.tscn Normal file
View File

@ -0,0 +1,14 @@
[gd_scene load_steps=2 format=3 uid="uid://b8vsmp8c1wqv6"]
[ext_resource type="Script" path="res://Field/tile.gd" id="1_kltr3"]
[node name="Tile" type="Node2D"]
script = ExtResource("1_kltr3")
[node name="Base" type="Sprite2D" parent="."]
scale = Vector2(1.45, 1.45)
flip_v = true
[node name="Sprite2D" type="Sprite2D" parent="."]
scale = Vector2(1.2, 1.2)
flip_v = true