sunoku/scenes/inventory/inventory.gd
2025-04-26 13:36:09 -07:00

124 lines
3.4 KiB
GDScript

extends Control
var items : Array[Array]
@onready var positive_modifiers = [$Speed,$BulletSpeed,$Ammunition,$Damage]
@onready var negative_modifiers = [$IncomingDamage,$FiringDelay,$Size,$TaxRate]
var item_offset : int = 26
@export var bonus_scene : PackedScene
@export var item_scene : PackedScene
@export var items_start : Node2D
@export var player : CharacterBody2D
func _ready():
items.append([])
items.append([])
for y in range(4):
items[0].append([])
items[1].append([])
for x in range(4):
var new_item = item_scene.instantiate()
new_item.set_value(abs(y-x)+1)
new_item.position = items_start.position + Vector2(x*item_offset,y*item_offset)
new_item.last_position = Vector2i(x,y)
new_item.inventory = self
items[0][y].append(new_item)
add_child(items[0][y][x])
if x == y:
var new_bonus = bonus_scene.instantiate()
new_bonus.position = items_start.position + Vector2(x*item_offset,y*item_offset)
new_bonus.last_position = Vector2i(x,y)
new_bonus.inventory = self
items[1][y].append(new_bonus)
add_child(items[1][y][x])
else:
items[1][y].append(null)
update_calculations()
func update_calculations():
for y in range(4):
var total = 0
for x in range(4):
if items[0][y][x] != null and items[1][y][x] == null:
total += items[0][y][x]._value
if total == 0:
total = 1
for x in range(4):
if items[1][y][x] != null:
total = items[1][y][x].calculate_bonus(total, items[0][y][x]._value)
match y:
0:
player.speed = total
1:
player.bullet_speed = total
2:
player.ammunition = total
3:
player.damage = total
positive_modifiers[y].text = '=' + str(total)
for x in range(4):
var total = 0
for y in range(4):
if items[0][y][x] != null and items[1][y][x] == null:
total += items[0][y][x]._value
if total == 0:
total = 1
for y in range(4):
if items[1][y][x] != null:
total = items[1][y][x].calculate_bonus(total, items[0][y][x]._value)
match x:
0:
player.incoming_damage = total
1:
player.bullet_delay = total
2:
player.size = total
3:
player.tax = total
negative_modifiers[x].text = '=' + str(total)
func inventory_to_global_coords(position : Vector2i) -> Vector2:
return items_start.global_position + Vector2(position.x*item_offset,position.y*item_offset)
func global_to_inventory_coords(position : Vector2):
position -= (items_start.global_position-Vector2(item_offset/2,item_offset/2))
position = Vector2i(position)
if position.x < 0 or position.y < 0:
return null
position /= item_offset
position = Vector2i(position)
if position.x >= 4 or position.y >= 4:
return null
return position
func _process(delta: float) -> void:
pass
func _input(event):
if event is InputEventMouseButton:
var layer = 0
if (event.button_index == MOUSE_BUTTON_LEFT or event.button_index == MOUSE_BUTTON_RIGHT) and event.pressed:
if(event.button_index == MOUSE_BUTTON_RIGHT):
layer = 1
var inventory_position = global_to_inventory_coords(event.position)
if inventory_position != null and items[layer][inventory_position.y][inventory_position.x] != null:
items[layer][inventory_position.y][inventory_position.x].dragging = true
items[layer][inventory_position.y][inventory_position.x].last_position = Vector2i(inventory_position.x,inventory_position.y)
items[layer][inventory_position.y][inventory_position.x] = null