Skip to content

Commit

Permalink
Merge pull request #214 from peter-kish/dev_v2.4.9
Browse files Browse the repository at this point in the history
GLoot - 2.4.9
  • Loading branch information
peter-kish authored Jun 1, 2024
2 parents e34dfc9 + d1b3c37 commit ca19ca8
Show file tree
Hide file tree
Showing 26 changed files with 570 additions and 117 deletions.
13 changes: 9 additions & 4 deletions addons/gloot/core/constraints/constraint_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ func _init(inventory_: Inventory) -> void:

func _on_item_added(item: InventoryItem) -> void:
assert(_enforce_constraints(item), "Failed to enforce constraints!")

# Enforcing constraints can result in the item being removed from the inventory
# (e.g. when it's merged with another item stack)
if !is_instance_valid(item.get_inventory()) || item.is_queued_for_deletion():
item = null

if _weight_constraint != null:
_weight_constraint._on_item_added(item)
Expand All @@ -52,13 +57,13 @@ func _on_item_removed(item: InventoryItem) -> void:
_grid_constraint._on_item_removed(item)


func _on_item_modified(item: InventoryItem) -> void:
func _on_item_property_changed(item: InventoryItem, property_name: String) -> void:
if _weight_constraint != null:
_weight_constraint._on_item_modified(item)
_weight_constraint._on_item_property_changed(item, property_name)
if _stacks_constraint != null:
_stacks_constraint._on_item_modified(item)
_stacks_constraint._on_item_property_changed(item, property_name)
if _grid_constraint != null:
_grid_constraint._on_item_modified(item)
_grid_constraint._on_item_property_changed(item, property_name)


func _on_pre_item_swap(item1: InventoryItem, item2: InventoryItem) -> bool:
Expand Down
63 changes: 30 additions & 33 deletions addons/gloot/core/constraints/grid_constraint.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ signal size_changed
const Verify = preload("res://addons/gloot/core/verify.gd")
const GridConstraint = preload("res://addons/gloot/core/constraints/grid_constraint.gd")
const StacksConstraint = preload("res://addons/gloot/core/constraints/stacks_constraint.gd")
const ItemMap = preload("res://addons/gloot/core/constraints/item_map.gd")
const QuadTree = preload("res://addons/gloot/core/constraints/quadtree.gd")
const Utils = preload("res://addons/gloot/core/utils.gd")

# TODO: Replace KEY_WIDTH and KEY_HEIGHT with KEY_SIZE
const KEY_WIDTH: String = "width"
Expand All @@ -16,8 +17,8 @@ const KEY_POSITIVE_ROTATION: String = "positive_rotation"
const KEY_GRID_POSITION: String = "grid_position"
const DEFAULT_SIZE: Vector2i = Vector2i(10, 10)

var _item_map := ItemMap.new(Vector2i.ZERO)
var _swap_position := Vector2i.ZERO
var _quad_tree := QuadTree.new(size)

@export var size: Vector2i = DEFAULT_SIZE :
set(new_size):
Expand All @@ -30,36 +31,41 @@ var _swap_position := Vector2i.ZERO
if _bounds_broken():
size = old_size
if size != old_size:
_refresh_item_map()
_refresh_quad_tree()
size_changed.emit()


func _refresh_item_map() -> void:
_item_map.resize(size)
_fill_item_map()


func _fill_item_map() -> void:
func _refresh_quad_tree() -> void:
_quad_tree = QuadTree.new(size)
for item in inventory.get_items():
_item_map.fill_rect(get_item_rect(item), item)
_quad_tree.add(get_item_rect(item), item)


func _on_inventory_set() -> void:
_refresh_item_map()
_refresh_quad_tree()


func _on_item_added(item: InventoryItem) -> void:
if item == null:
return
_item_map.fill_rect(get_item_rect(item), item)
_quad_tree.add(get_item_rect(item), item)


func _on_item_removed(item: InventoryItem) -> void:
_item_map.clear_rect(get_item_rect(item))
_quad_tree.remove(item)


func _on_item_modified(item: InventoryItem) -> void:
_refresh_item_map()
func _on_item_property_changed(item: InventoryItem, property_name: String) -> void:
var relevant_properties = [
KEY_SIZE,
KEY_ROTATED,
KEY_WIDTH,
KEY_HEIGHT,
KEY_GRID_POSITION,
]
if property_name in relevant_properties:
_quad_tree.remove(item)
_quad_tree.add(get_item_rect(item), item)


func _on_pre_item_swap(item1: InventoryItem, item2: InventoryItem) -> bool:
Expand All @@ -78,7 +84,7 @@ func _size_check(item1: InventoryItem, item2: InventoryItem) -> bool:
var grid_constraint1: GridConstraint = null
if is_instance_valid(inv1):
grid_constraint1 = inv1._constraint_manager.get_grid_constraint()
var inv2 = item1.get_inventory()
var inv2 = item2.get_inventory()
var grid_constraint2: GridConstraint = null
if is_instance_valid(inv2):
grid_constraint2 = inv2._constraint_manager.get_grid_constraint()
Expand Down Expand Up @@ -261,10 +267,10 @@ func create_and_add_item_at(prototype_id: String, position: Vector2i) -> Invento

func get_item_at(position: Vector2i) -> InventoryItem:
assert(inventory != null, "Inventory not set!")

if !_item_map.contains(position):
var first = _quad_tree.get_first(position)
if first == null:
return null
return _item_map.get_field(position)
return first.metadata


func get_items_under(rect: Rect2i) -> Array[InventoryItem]:
Expand Down Expand Up @@ -297,7 +303,8 @@ func move_item_to_free_spot(item: InventoryItem) -> bool:
if not free_place.success:
return false

return move_item_to(item, free_place.position)
_move_item_to_unsafe(item, free_place.position)
return true


func _move_item_to_unsafe(item: InventoryItem, position: Vector2i) -> void:
Expand Down Expand Up @@ -362,12 +369,7 @@ func rect_free(rect: Rect2i, exception: InventoryItem = null) -> bool:
if rect.position.y + rect.size.y > size.y:
return false

for i in range(rect.position.x, rect.position.x + rect.size.x):
for j in range(rect.position.y, rect.position.y + rect.size.y):
var field = _item_map.get_field(Vector2i(i, j))
if field != null && field != exception:
return false
return true
return _quad_tree.get_first(rect, exception) == null


# TODO: Check if this is needed after adding find_free_space
Expand Down Expand Up @@ -419,8 +421,6 @@ func _sort_if_needed() -> void:
func get_space_for(item: InventoryItem) -> ItemCount:
var occupied_rects: Array[Rect2i]
var item_size = get_item_size(item)
if item_size == Vector2i.ONE:
return ItemCount.new(_item_map.free_fields)

var free_space := find_free_space(item_size, occupied_rects)
while free_space.success:
Expand All @@ -430,10 +430,7 @@ func get_space_for(item: InventoryItem) -> ItemCount:


func has_space_for(item: InventoryItem) -> bool:
var item_size = get_item_size(item)
if item_size == Vector2i.ONE:
return _item_map.free_fields > 0

var item_size = get_item_size(item)
return find_free_space(item_size).success


Expand Down Expand Up @@ -477,7 +474,7 @@ func deserialize(source: Dictionary) -> bool:

reset()

var s: Vector2i = str_to_var(source[KEY_SIZE])
var s: Vector2i = Utils.str_to_var(source[KEY_SIZE])
self.size = s

return true
Expand Down
4 changes: 2 additions & 2 deletions addons/gloot/core/constraints/inventory_constraint.gd
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ func _on_item_added(item: InventoryItem) -> void:
func _on_item_removed(item: InventoryItem) -> void:
pass


# Override this
func _on_item_modified(item: InventoryItem) -> void:
func _on_item_property_changed(item: InventoryItem, property_name: String) -> void:
pass


Expand Down
Loading

0 comments on commit ca19ca8

Please sign in to comment.