WindowPuzzler/scripts/world.gd

39 lines
1.3 KiB
GDScript

extends Node2D
@onready var WorldTiles : TileMapLayer = $TileMapLayer
var chunkPos : Vector2i = Vector2i(0,0)
enum {WALL, AIR}
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
get_tree().auto_accept_quit = false
load_game("Chunk"+str(chunkPos.x)+"_"+str(chunkPos.y), WorldTiles)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
var setPos : Vector2i = WorldTiles.local_to_map(get_local_mouse_position())
WorldTiles.set_cell(setPos,WALL,Vector2i(0,0))
if Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT):
var setPos : Vector2i = WorldTiles.local_to_map(get_local_mouse_position())
WorldTiles.set_cell(setPos,AIR,Vector2i(0,0))
func load_game(path:String, tile_map_layer:TileMapLayer) -> void:
var file : FileAccess = FileAccess.open(path, FileAccess.READ)
if file:
tile_map_layer.tile_map_data = file.get_var()
func save(path:String, tile_map_layer:TileMapLayer) -> void:
var file : FileAccess = FileAccess.open(path, FileAccess.WRITE)
file.store_var(tile_map_layer.tile_map_data)
file.close()
func _notification(what: int) -> void:
if what == NOTIFICATION_WM_CLOSE_REQUEST:
save("Chunk"+str(chunkPos.x)+"_"+str(chunkPos.y), WorldTiles)
get_tree().quit()