summaryrefslogtreecommitdiff
path: root/scripts/player.gd
blob: fee48365662c814d1f84a50f7b7a4146471a81b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
extends Node

const Items = preload("res://scripts/items.gd")
var items

var cash = 100 setget set_cash, get_cash
var storage = {}
var inventory = {}
  
func _ready():
  self.items = Items.new()
  _update_funds()
  
func get_cash():
  return cash

func set_cash(csh):
  cash = csh
  _update_funds()
  
func add_item(id, amount):
  if storage.has(id):
    storage[id] += amount
  else:
    storage[id] = amount
  inventory[id] = items.get(id)
  _update_storage(id, storage[id])

func has_item(id, amount=1):
  return storage.has(id) and storage[id] >= amount
  
func get_item(id):
  return inventory[id]

func get_item_count(id):
  return storage[id]
  
func remove_item(id, amount):
  if storage.has(id) and storage[id] >= amount:
    storage[id] -= amount
    _update_storage(id, storage[id])

func _update_funds():
  get_tree().get_root().get_node("Game/HUD").set_funds(cash)

func _update_storage(id, amount):
  get_tree().get_root().get_node("Game/HUD").update_storage(id, amount)