-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackpack.gd
95 lines (78 loc) · 2.52 KB
/
Backpack.gd
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
extends Node
signal item_added(item, index)
export(String) var Name = "Inventory Name"
export(String, FILE) var DefinitionFilePath = "res://inventory_definition.json"
export(int) var Width = 3
export(int) var Height = 3
onready var grid = []
var definitions
func _ready():
# loading inventory definition from json file
definitions = load_definition_from_json(DefinitionFilePath)
if not definitions:
push_error("failed to get inventory definition, exiting the game")
get_tree().quit() # TODO provide a default definition?
return
# generate inventory grid
for i in Width * Height:
grid.append([])
func add_item(item):
# TODO
# 1. get item stack size from the definition
# 2. check item.id == stack[index].id
# 2.1. true: add to the stack
# 2.2 false: continue (next stack)
for i in len(grid):
if grid[i].size() == 0:
grid[i].append(item)
print("appended %s at position %d" % [definitions[item.item_id], i])
emit_signal("item_added", item, i)
break
func get_item(position):
if position.x < Width and position.y < Height:
var stack_at_position = grid[get_grid_index(position)]
if stack_at_position:
return stack_at_position[0]
func add_stack_to(stack, position):
grid[get_grid_index(position)].append_array(stack)
print(to_string())
func remove_stack_from(position):
grid[get_grid_index(position)] = []
print(to_string())
func load_definition_from_json(file_path):
var file = File.new()
var err = file.open(file_path, File.READ)
if err:
var err_msg = "error opening dictionary, err no: {no}"
err_msg += ".did you provide a valid DefinitionFilePath to {name}?"
push_error(err_msg.format({"name": name, "no": err}))
return null
var content = file.get_as_text()
file.close()
var parsed = JSON.parse(content)
if parsed.error:
var format_error = "load_dictionary_from_json failure "
format_error += "err no: {errno}, {errstring} at line: {errline}"
var error = format_error.format(
{"errno": str(parsed.error),
"errstring": str(parsed.error_string),
"errline": str(parsed.error_line)})
push_error(error)
return null
return parsed.result
func get_grid_position(index):
return Vector2(index % Width, index / Width)
func get_grid_index(vec2):
return vec2.y * Width + vec2.x
func to_string():
var print_value = ""
for index in Width * Height:
var cell = "[%d](%d,%d):%s\t"
print_value += cell % \
[index,
get_grid_position(index).x,
get_grid_position(index).y,
grid[index]]
if index and not ((index+1) % Width):
print_value += "\n"
return print_value