-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContainerField.gd
93 lines (72 loc) · 2.31 KB
/
ContainerField.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
extends Spatial
export (int) var numRows
export (int) var containersPerRow
export (int) var stackHeight
export (int) var numGenerateContainers = 3
const Containerz = preload("res://Container.tscn")
const CONTAINER_WIDTH = 6.09
const CONTAINER_HEIGHT = 2.59
const CONTAINER_DEPTH = 2.44
export (float) var ROW_PADDING = 1.5
export (float) var COLUMN_PADDING = 5.0 / 16
export (bool) var inUse = false
class ContainerSpot:
var index: int
var position: Vector3
var container = null
func _init(i, pos, c):
index = i
position = pos
container = c
# 1D array (numRows * containersPerRow) of stacks (last element = topmost)
var stacks = []
var random_indices = []
func _init():
pass
func _ready():
random_generate()
func random_generate():
stacks.resize(numRows * containersPerRow)
random_indices.resize(numRows * containersPerRow)
for i in range(numRows * containersPerRow):
random_indices[i] = i
stacks[i] = []
for i in range(numGenerateContainers):
var spot = find_container_spot()
if spot == null:
break
spot.container = Containerz.instance()
put_container(spot)
func get_pos(i: int, stackNum = 0):
var column = i % containersPerRow
var row = i / containersPerRow
var x = (CONTAINER_DEPTH + ROW_PADDING) * numRows * -0.5 + ROW_PADDING + (CONTAINER_DEPTH + ROW_PADDING) * row
var z = (CONTAINER_WIDTH + COLUMN_PADDING) * containersPerRow * -0.5 + ROW_PADDING + (CONTAINER_WIDTH + COLUMN_PADDING) * column
return Vector3(x, stackNum * CONTAINER_HEIGHT, z)
func find_topmost_container():
random_indices.shuffle()
for i in random_indices:
if stacks[i].size() > 0:
return stacks[i][stacks[i].size() - 1]
return null
func find_container_spot():
random_indices.shuffle()
for i in random_indices:
if stacks[i].size() < stackHeight:
return ContainerSpot.new(i, get_pos(i, stacks[i].size()), null)
return null
func obtain(spot: ContainerSpot):
if stacks[spot.index].size() == 0:
return null
return stacks[spot.index].pop_back()
func put_container(spot: ContainerSpot):
if spot.container == null:
return null
spot.position = get_pos(spot.index, stacks[spot.index].size())
spot.container.transform.origin = spot.position
var parent = spot.container.get_parent()
if parent != null:
parent.remove_child(spot.container)
add_child(spot.container)
stacks[spot.index].append(spot)
return spot