Added player character and initial interaction functionality
This commit is contained in:
35
scripts/autoloads/grid.gd
Normal file
35
scripts/autoloads/grid.gd
Normal file
@@ -0,0 +1,35 @@
|
||||
extends Node
|
||||
|
||||
const GRID_SIZE := 64
|
||||
var world_grid: TileMap
|
||||
var world_data: Dictionary = {}
|
||||
|
||||
func init(grid: TileMap) -> void:
|
||||
world_grid = grid
|
||||
|
||||
func world_to_grid(world_loc: Vector2) -> Vector2i:
|
||||
return Vector2i(floor(world_loc.x / GRID_SIZE), floor(world_loc.y / GRID_SIZE))
|
||||
|
||||
func set_selected_tile(tile: Vector2i) -> void:
|
||||
world_grid.clear_layer(Constants.TilemapLayers.CURSOR)
|
||||
world_grid.set_cell(Constants.TilemapLayers.CURSOR, tile,0,Vector2i(0,7))
|
||||
|
||||
func corrupt_location(loc: Vector2i) -> bool:
|
||||
return world_data[loc].change_layer(Constants.TilemapLayers.CORRUPTION, true)
|
||||
|
||||
func get_corrupted_cells() -> Array[Vector2i]:
|
||||
return world_data.values().filter(func(cell: CellData): return cell.is_corrupted()).map(func(cell: CellData) -> Vector2i: return cell._pos)
|
||||
|
||||
func change_location_data(pos: Vector2i, layer: int, data: Vector2i) -> void:
|
||||
if not world_data.has(pos):
|
||||
world_data[pos] = CellData.new(pos)
|
||||
world_data[pos].change_layer(layer, data)
|
||||
world_grid.set_cell(layer, pos, 0, data)
|
||||
|
||||
func get_location_data(pos: Vector2i) -> CellData:
|
||||
if world_data.has(pos):
|
||||
return world_data[pos]
|
||||
return null
|
||||
|
||||
func change_location_resource(pos: Vector2i, data: GameResource) -> void:
|
||||
world_data[pos].change_resource(data)
|
||||
@@ -6,6 +6,9 @@ extends Node
|
||||
}
|
||||
var _pos: Vector2i
|
||||
|
||||
func _to_string() -> String:
|
||||
return "{%s %s}" % [_pos, layer_info]
|
||||
|
||||
func _init(pos: Vector2i) -> void:
|
||||
_pos = pos
|
||||
|
||||
|
||||
@@ -6,3 +6,6 @@ extends Resource
|
||||
@export_range(-1, 1, 0.05) var spawn_region_max: float
|
||||
@export var pickup_value: int
|
||||
@export var name: String
|
||||
|
||||
func _to_string() -> String:
|
||||
return name
|
||||
|
||||
27
scripts/player.gd
Normal file
27
scripts/player.gd
Normal file
@@ -0,0 +1,27 @@
|
||||
extends CharacterBody2D
|
||||
class_name Player
|
||||
|
||||
@export var SPEED := 4
|
||||
|
||||
var last_direction = 0
|
||||
var interaction_location: Vector2i
|
||||
|
||||
@onready var animation_player: AnimationPlayer = $AnimationPlayer
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
var input_direction := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
|
||||
velocity = input_direction * SPEED / delta
|
||||
move_and_slide()
|
||||
|
||||
if input_direction:
|
||||
animation_player.play()
|
||||
last_direction = input_direction.angle() + 3 * PI / 2
|
||||
rotation = last_direction
|
||||
interaction_location = Grid.world_to_grid(position) + Vector2i(input_direction.round())
|
||||
Grid.set_selected_tile(interaction_location)
|
||||
else:
|
||||
animation_player.stop()
|
||||
|
||||
func _input(event: InputEvent) -> void:
|
||||
if event.is_action_pressed("ui_accept"):
|
||||
print("Interacted with: %s" % Grid.get_location_data(interaction_location))
|
||||
@@ -6,14 +6,14 @@ signal changed_resource
|
||||
|
||||
@export var resources := {}
|
||||
|
||||
func _on_timer_timeout() -> void:
|
||||
var corrupted_resources := world.get_corrupted_resources()
|
||||
if corrupted_resources.size() > 0:
|
||||
var changed_resources := {}
|
||||
for corrupted_resource: CellData in corrupted_resources:
|
||||
var res: GameResource = corrupted_resource.get_resource()
|
||||
if not resources.has(res):
|
||||
resources[res] = 0
|
||||
resources[res] += res.pickup_value
|
||||
changed_resources[res] = resources[res]
|
||||
changed_resource.emit(ResourceChangedSignal.new(changed_resources))
|
||||
#func _on_timer_timeout() -> void:
|
||||
#var corrupted_resources := Grid.get_corrupted_resources()
|
||||
#if corrupted_resources.size() > 0:
|
||||
#var changed_resources := {}
|
||||
#for corrupted_resource: CellData in corrupted_resources:
|
||||
#var res: GameResource = corrupted_resource.get_resource()
|
||||
#if not resources.has(res):
|
||||
#resources[res] = 0
|
||||
#resources[res] += res.pickup_value
|
||||
#changed_resources[res] = resources[res]
|
||||
#changed_resource.emit(ResourceChangedSignal.new(changed_resources))
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
class_name World extends Node2D
|
||||
|
||||
@export var game_resources: Array[GameResource]
|
||||
const PLAYER = preload("res://scene/player.tscn")
|
||||
|
||||
var terrain_noise := FastNoiseLite.new()
|
||||
var resource_noise: Noise = FastNoiseLite.new()
|
||||
@@ -12,8 +13,6 @@ var temp_noise_reg = []
|
||||
var noise_regions := [-999,0,999]
|
||||
var atlas_regions := [Vector2i(0,1),Vector2i(0,0)]
|
||||
|
||||
var world_data: Dictionary = {}
|
||||
|
||||
@onready var world_grid: TileMap = $world_grid
|
||||
|
||||
func get_noise_value(x: int, y: int):
|
||||
@@ -33,12 +32,13 @@ func get_noise_region(x,y):
|
||||
return i
|
||||
|
||||
func _ready():
|
||||
Grid.init(world_grid)
|
||||
terrain_noise.seed = randi()
|
||||
resource_noise.seed = randi()
|
||||
|
||||
for x in range(-temp_size, temp_size):
|
||||
for y in range(-temp_size, temp_size):
|
||||
change_location_data(Vector2i(x,y), Constants.TilemapLayers.GROUND, atlas_regions[get_noise_region(x,y)])
|
||||
Grid.change_location_data(Vector2i(x,y), Constants.TilemapLayers.GROUND, atlas_regions[get_noise_region(x,y)])
|
||||
var tile_res := is_resource_tile(x,y)
|
||||
if tile_res:
|
||||
change_location_resource(Vector2i(x,y), tile_res)
|
||||
@@ -47,29 +47,17 @@ func _ready():
|
||||
prints(temp_noise_val.min(), temp_noise_val.max())
|
||||
for region in noise_regions.size():
|
||||
prints(region, noise_regions[region], temp_noise_reg.count(region))
|
||||
|
||||
func _process(delta):
|
||||
var mouse_pos = world_grid.local_to_map(get_local_mouse_position())
|
||||
world_grid.clear_layer(Constants.TilemapLayers.CURSOR)
|
||||
world_grid.set_cell(Constants.TilemapLayers.CURSOR, mouse_pos,0,Vector2i(0,7))
|
||||
var player = PLAYER.instantiate()
|
||||
add_child(player)
|
||||
|
||||
func corrupt_location(loc: Vector2i):
|
||||
if world_data[loc].change_layer(Constants.TilemapLayers.CORRUPTION, true):
|
||||
var corrupted_cells := world_data.values().filter(func(cell: CellData): return cell.is_corrupted()).map(func(cell: CellData) -> Vector2i: return cell._pos)
|
||||
if Grid.corrupt_location(loc):
|
||||
var corrupted_cells := Grid.get_corrupted_cells()
|
||||
world_grid.set_cells_terrain_connect(Constants.TilemapLayers.CORRUPTION, corrupted_cells, 0, 0)
|
||||
|
||||
func grid_location(global_pos: Vector2) -> Vector2i:
|
||||
return world_grid.local_to_map(global_pos)
|
||||
|
||||
func change_location_data(pos: Vector2i, layer: int, data: Vector2i) -> void:
|
||||
if not world_data.has(pos):
|
||||
world_data[pos] = CellData.new(pos)
|
||||
world_data[pos].change_layer(layer, data)
|
||||
world_grid.set_cell(layer, pos, 0, data)
|
||||
|
||||
func change_location_resource(pos: Vector2i, data: GameResource) -> void:
|
||||
world_data[pos].change_resource(data)
|
||||
Grid.change_location_resource(pos, data)
|
||||
world_grid.set_cell(Constants.TilemapLayers.ENVIRONMENT, pos, 0, data.atlas_location)
|
||||
|
||||
func get_corrupted_resources() -> Array:
|
||||
return world_data.values().filter(func(cell: CellData): return cell.is_corrupted() and cell.has_layer(Constants.TilemapLayers.ENVIRONMENT))
|
||||
|
||||
Reference in New Issue
Block a user