Plant spawn
This commit is contained in:
parent
32453f2e9d
commit
941912d7f1
25 changed files with 281 additions and 40 deletions
|
|
@ -20,6 +20,11 @@ signal entity_killed(context : Entity.KilledContext)
|
|||
## Called for every entity that gets damage
|
||||
signal entity_hp_changed(context : Entity.HPChangedContext)
|
||||
|
||||
## Called for every entity that has layer set that enters game
|
||||
signal layer_entity_created(entity : Entity)
|
||||
## Called for every entity that has layer set that exits game
|
||||
signal layer_entity_killed(entity : Entity.KilledContext)
|
||||
|
||||
#endregion
|
||||
|
||||
#region Seedpacket manipulation
|
||||
|
|
@ -27,6 +32,9 @@ signal entity_hp_changed(context : Entity.HPChangedContext)
|
|||
## Called when player selects SeedpacketResource
|
||||
signal packet_selected(resource : SeedpacketResource)
|
||||
|
||||
## Called when player deselects (usually loses focus) SeedpacketResource
|
||||
signal packet_deselected(resource : SeedpacketResource)
|
||||
|
||||
## Called when selected packets are updated
|
||||
signal hotbar_packets_update(selected : Array[SeedpacketResource])
|
||||
#endregion
|
||||
|
|
|
|||
4
scripts/autoloads/player_progress.gd
Normal file
4
scripts/autoloads/player_progress.gd
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
extends Node
|
||||
|
||||
var seedpacket_count : int
|
||||
var owned_plants : Array[SeedpacketResource]
|
||||
1
scripts/autoloads/player_progress.gd.uid
Normal file
1
scripts/autoloads/player_progress.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://cjxsn8khrawb4
|
||||
|
|
@ -5,6 +5,8 @@ class_name Entity
|
|||
|
||||
## Maximum health points of an entity. Any heal cannot exceed this value
|
||||
@export var max_hp : float
|
||||
## Optional spawn layer for grid interactions
|
||||
@export var layer : StringName = ""
|
||||
## Current amount of health points of an entity. Cannot be below 0 or [code]max_hp[/code]
|
||||
var hp : float = max_hp
|
||||
|
||||
|
|
@ -67,6 +69,10 @@ func kill(source : Entity):
|
|||
context.target = self
|
||||
killed.emit(context)
|
||||
|
||||
LevelEventBus.entity_killed.emit(context)
|
||||
if not layer.is_empty():
|
||||
LevelEventBus.layer_entity_killed.emit(context)
|
||||
|
||||
deconstruct()
|
||||
|
||||
|
||||
|
|
|
|||
5
scripts/gui/plant_pick/go_button.gd
Normal file
5
scripts/gui/plant_pick/go_button.gd
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
extends Button
|
||||
|
||||
|
||||
func _pressed() -> void:
|
||||
LevelEventBus.state_advance_requested.emit()
|
||||
1
scripts/gui/plant_pick/go_button.gd.uid
Normal file
1
scripts/gui/plant_pick/go_button.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://60lwfjb634kd
|
||||
|
|
@ -29,4 +29,4 @@ func on_level_state_changed(state : LevelData.LevelStates):
|
|||
|
||||
func on_sun_count_updated(to : float):
|
||||
enough_sun = to >= seedpacket.held_resource.cost
|
||||
|
||||
seedpacket.update_contents()
|
||||
|
|
|
|||
|
|
@ -16,6 +16,9 @@ class_name Seedpacket
|
|||
var held_resource : SeedpacketResource
|
||||
var handler : SeedpacketHandler
|
||||
|
||||
func _ready() -> void:
|
||||
focus_exited.connect(on_focus_exited)
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
button.disabled = recharge_timer.time_left > 0 or handler.is_avaiable() == false
|
||||
|
||||
|
|
@ -45,3 +48,6 @@ func on_packet_placed(packet : SeedpacketResource):
|
|||
func disconnect_placement():
|
||||
LevelEventBus.packet_placed.disconnect(on_packet_placed)
|
||||
focus_exited.disconnect(disconnect_placement)
|
||||
|
||||
func on_focus_exited():
|
||||
LevelEventBus.packet_deselected.emit(held_resource)
|
||||
|
|
|
|||
6
scripts/level/field_rect_offsetter.gd
Normal file
6
scripts/level/field_rect_offsetter.gd
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
extends Marker2D
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
FieldParams.field_rect.position = global_position
|
||||
queue_free()
|
||||
1
scripts/level/field_rect_offsetter.gd.uid
Normal file
1
scripts/level/field_rect_offsetter.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://cj8mt02far5np
|
||||
37
scripts/level/layered_entity_container.gd
Normal file
37
scripts/level/layered_entity_container.gd
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
extends Node
|
||||
|
||||
class_name LayeredEntityContainer
|
||||
|
||||
var entities := {
|
||||
"under" : [],
|
||||
"base" : [],
|
||||
"above" : []
|
||||
}
|
||||
|
||||
func _ready() -> void:
|
||||
LevelEventBus.layer_entity_created.connect(on_entity_spawned)
|
||||
LevelEventBus.layer_entity_killed.connect(on_entity_killed)
|
||||
|
||||
for layer in entities.keys():
|
||||
entities[layer].resize(FieldParams.COLUMNS * FieldParams.ROWS)
|
||||
|
||||
## Checks if tile is occupied. [br]
|
||||
## If position or layer are invalid, returns true
|
||||
func is_occupied(position : int, layer : StringName) -> bool:
|
||||
if position >= FieldParams.COLUMNS * FieldParams.ROWS or entities.has(layer) == false: return true
|
||||
return not entities[layer][position] == null
|
||||
|
||||
func on_entity_spawned(entity : Entity) -> void:
|
||||
if entities.has(entity.layer) == false: return
|
||||
|
||||
var index = FieldParams.indexify(entity.get_parent().global_position)
|
||||
if index >= FieldParams.COLUMNS * FieldParams.ROWS or is_occupied(index,entity.layer): return
|
||||
|
||||
entities[entity.layer][index] = entity
|
||||
|
||||
func on_entity_killed(context : Entity.KilledContext) -> void:
|
||||
var entity = context.target
|
||||
var index = FieldParams.indexify(entity.get_parent().global_position)
|
||||
if is_occupied(index,entity.layer) == false: return
|
||||
|
||||
entities[entity.layer][index] = null
|
||||
1
scripts/level/layered_entity_container.gd.uid
Normal file
1
scripts/level/layered_entity_container.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://cyttjwv888cb4
|
||||
|
|
@ -3,12 +3,35 @@ extends Node
|
|||
class_name LevelData
|
||||
|
||||
static var state : LevelStates = LevelStates.NotInGame
|
||||
static var sun_count : float
|
||||
static var sun_count : float = 100
|
||||
|
||||
var hotbar_seedpackets : Array[SeedpacketResource]
|
||||
|
||||
func _ready() -> void:
|
||||
LevelEventBus.packet_selected.connect(on_seedpacket_clicked)
|
||||
set_state(LevelStates.PlantPick)
|
||||
LevelEventBus.state_advance_requested.connect(advance_state)
|
||||
LevelEventBus.packet_placed.connect(on_packet_placed)
|
||||
|
||||
func _exit_tree() -> void:
|
||||
set_state(LevelStates.NotInGame)
|
||||
|
||||
func advance_state() -> void:
|
||||
set_state(state + 1)
|
||||
|
||||
func set_state(to : LevelStates):
|
||||
state = to
|
||||
match state:
|
||||
LevelStates.PlantPick:
|
||||
LevelEventBus.packet_selected.connect(on_seedpacket_clicked)
|
||||
LevelStates.Pregame:
|
||||
LevelEventBus.packet_selected.disconnect(on_seedpacket_clicked)
|
||||
LevelStates.Game:
|
||||
pass
|
||||
LevelStates.Postgame:
|
||||
pass
|
||||
LevelStates.NotInGame:
|
||||
pass
|
||||
LevelEventBus.state_changed.emit(state)
|
||||
|
||||
func on_seedpacket_clicked(seedpacket : SeedpacketResource):
|
||||
if hotbar_seedpackets.has(seedpacket):
|
||||
|
|
@ -17,6 +40,11 @@ func on_seedpacket_clicked(seedpacket : SeedpacketResource):
|
|||
hotbar_seedpackets.append(seedpacket)
|
||||
LevelEventBus.hotbar_packets_update.emit(hotbar_seedpackets)
|
||||
|
||||
func on_packet_placed(seedpacket : SeedpacketResource):
|
||||
print(seedpacket)
|
||||
sun_count -= seedpacket.cost
|
||||
LevelEventBus.sun_count_updated.emit(sun_count)
|
||||
|
||||
## Possible states of level
|
||||
enum LevelStates {
|
||||
## The game is during plant pick stage
|
||||
|
|
@ -27,6 +55,6 @@ enum LevelStates {
|
|||
Game,
|
||||
## Game ended
|
||||
Postgame,
|
||||
## Not in a level,
|
||||
## Not in a level
|
||||
NotInGame
|
||||
}
|
||||
|
|
|
|||
51
scripts/level/seedpacket_placer.gd
Normal file
51
scripts/level/seedpacket_placer.gd
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
extends CanvasGroup
|
||||
|
||||
@export var entity_container : LayeredEntityContainer
|
||||
var can_plant : bool = false
|
||||
var held_packet : SeedpacketResource = null
|
||||
|
||||
func _ready() -> void:
|
||||
LevelEventBus.state_changed.connect(on_level_state_changed)
|
||||
LevelEventBus.packet_deselected.connect(on_seedpacket_deselected)
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
var mouse_pos = get_global_mouse_position()
|
||||
var indexified_mouse = FieldParams.indexify(mouse_pos - FieldParams.field_rect.position)
|
||||
if FieldParams.field_rect.has_point(mouse_pos) and entity_container.is_occupied(indexified_mouse,"base") == false:
|
||||
global_position = FieldParams.deindexify(indexified_mouse) + FieldParams.TILE / 2.0 + FieldParams.field_rect.position
|
||||
can_plant = true
|
||||
else:
|
||||
global_position = mouse_pos
|
||||
can_plant = false
|
||||
|
||||
func _input(event: InputEvent) -> void:
|
||||
if held_packet != null and event.is_action_pressed("cancel_action"):
|
||||
stop_placement()
|
||||
|
||||
if held_packet != null and can_plant and event.is_action_pressed("primary_action"):
|
||||
for child in get_children():
|
||||
child.reparent(get_tree().current_scene.get_node("%Plants"))
|
||||
LevelEventBus.packet_placed.emit(held_packet)
|
||||
held_packet = null
|
||||
visible = false
|
||||
|
||||
func on_seedpacket_selected(packet : SeedpacketResource) -> void:
|
||||
add_child(packet.scene.instantiate())
|
||||
held_packet = packet
|
||||
visible = true
|
||||
|
||||
func on_seedpacket_deselected(packet : SeedpacketResource) -> void:
|
||||
if packet == held_packet:
|
||||
stop_placement()
|
||||
|
||||
func on_level_state_changed(state : LevelData.LevelStates) -> void:
|
||||
if state == LevelData.LevelStates.Game:
|
||||
LevelEventBus.state_changed.disconnect(on_level_state_changed)
|
||||
LevelEventBus.packet_selected.connect(on_seedpacket_selected)
|
||||
|
||||
|
||||
func stop_placement():
|
||||
for child in get_children():
|
||||
child.queue_free()
|
||||
held_packet = null
|
||||
visible = false
|
||||
1
scripts/level/seedpacket_placer.gd.uid
Normal file
1
scripts/level/seedpacket_placer.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://sjnxv5q4yk2
|
||||
23
scripts/utility/field_params.gd
Normal file
23
scripts/utility/field_params.gd
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
extends Node
|
||||
|
||||
## Utility class for field parameters and connected functions
|
||||
|
||||
class_name FieldParams
|
||||
|
||||
## Tile size
|
||||
const TILE : Vector2 = Vector2(50,60)
|
||||
## Rows count of field
|
||||
const ROWS : int = 5
|
||||
## Columns count of field
|
||||
const COLUMNS : int = 9
|
||||
|
||||
## Field rectangle. Origin is set in-game
|
||||
static var field_rect : Rect2 = Rect2(0,0,COLUMNS*TILE.x,ROWS*TILE.y)
|
||||
|
||||
## Converts vector position into int index. Hash somewhat
|
||||
static func indexify(position : Vector2) -> int:
|
||||
var tiled_position = (position/TILE).floor()
|
||||
return int(tiled_position.x) + int(tiled_position.y * COLUMNS)
|
||||
|
||||
static func deindexify(index : int) -> Vector2:
|
||||
return Vector2(index % COLUMNS * TILE.x, index / COLUMNS * TILE.y)
|
||||
1
scripts/utility/field_params.gd.uid
Normal file
1
scripts/utility/field_params.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://dnsvj0t3oe3ke
|
||||
Loading…
Add table
Add a link
Reference in a new issue