forked from matheusmaldaner/JasonJourney
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwave.py
151 lines (135 loc) · 5.51 KB
/
wave.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from wfc import State, Rule, Wave
# class shall be used to define rules that must be followed
# in the Wave Function Collapse algorithm for level generation
class WaveFunctionCollapse():
def __init__(self):
def repeat_state(state, count):
return [state] * count
# working as supposed
floor_repeat_count = 5
self.floor = State(
"floor",
Rule(
lambda x, y: {
(x, y - 1): floor_repeat_count * ["floor"] + 1 * ["top_wall"], # Above
(x - 1, y): floor_repeat_count * ["floor"] + 1 * ["left_wall"], # Left
(x + 1, y): floor_repeat_count * ["floor"] + 1 * ["right_wall"], # Right
(x, y + 1): floor_repeat_count * ["floor"] + 1 * ["bottom_wall"] # Below
}
)
)
# 4 wall sides
self.top_wall = State(
"top_wall",
Rule(
lambda x, y: {
(x, y - 1): ["floor", "air"], # Above
(x - 1, y): ["top_left_wall", "top_wall"], # Left
(x + 1, y): ["top_right_wall", "top_wall"], # Right
(x, y + 1): ["floor", "air"] # Below
}
)
)
self.left_wall = State(
"left_wall",
Rule(
lambda x, y: {
(x, y - 1): ["top_left_wall", "left_wall", "left_wall" "left_wall"], # Above
(x - 1, y): ["floor", "air"], # Left
(x + 1, y): ["floor", "air"], # Right
(x, y + 1): ["bottom_left_wall", "left_wall", "left_wall", "left_wall"] # Below
}
)
)
self.right_wall = State(
"right_wall",
Rule(
lambda x, y: {
(x, y - 1): ["top_right_wall", "right_wall", "right_wall", "right_wall"], # Above
(x - 1, y): ["floor", "air"], # Left
(x + 1, y): ["floor", "air"], # Right
(x, y + 1): ["bottom_right_wall", "right_wall", "right_wall", "right_wall"] # Below
}
)
)
self.bottom_wall = State(
"bottom_wall",
Rule(
lambda x, y: {
(x, y - 1): ["floor"], # Above
(x - 1, y): ["bottom_left_wall", "bottom_wall", "bottom_wall", "bottom_wall"], # Left
(x + 1, y): ["bottom_right_wall", "bottom_wall", "bottom_wall", "bottom_wall"], # Right
(x, y + 1): ["floor"] # Below
}
)
)
# four corners
self.top_left = State(
"top_left_wall",
Rule(
lambda x, y: {
(x, y - 1): ["floor"], # Above
(x - 1, y): ["floor"], # Left
(x + 1, y): ["top_wall"], # Right
(x, y + 1): ["left_wall"] # Below
}
)
)
self.top_right = State(
"top_right_wall",
Rule(
lambda x, y: {
(x, y - 1): ["floor"], # Above
(x - 1, y): ["top_wall"], # Left
(x + 1, y): ["floor"], # Right
(x, y + 1): ["right_wall"] # Below
}
)
)
self.bottom_left = State(
"bottom_left_wall",
Rule(
lambda x, y: {
(x, y - 1): ["left_wall"], # Above
(x - 1, y): ["floor"], # Left
(x + 1, y): ["bottom_wall"], # Right
(x, y + 1): ["floor"] # Below
}
)
)
self.bottom_right = State(
"bottom_right_wall",
Rule(
lambda x, y: {
(x, y - 1): ["right_wall"], # Above
(x - 1, y): ["bottom_wall"], # Left
(x + 1, y): ["floor"], # Right
(x, y + 1): ["floor"] # Below
}
)
)
self.air = State(
"air",
Rule(
lambda x, y: {
(x, y - 1): ["air", "bottom_wall"], # Above
(x - 1, y): ["air", "right_wall"], # Left
(x + 1, y): ["air", "left_wall"], # Right
(x, y + 1): ["air", "top_wall"] # Below
}
)
)
def collapse(self, x, y):
# Define the dimensions of your level (e.g., 10x40)
level_dimensions = (x, y)
# Create the wave with your states
level_wave = Wave(level_dimensions, [self.floor, self.top_left, self.top_right, self.bottom_left, self.bottom_right,
self.top_wall, self.right_wall, self.bottom_wall, self.left_wall,
self.top_wall, self.right_wall, self.bottom_wall, self.left_wall,
self.floor, self.floor, self.floor, self.floor, self.floor, self.floor,
self.floor, self.floor, self.floor, self.floor, self.floor, self.floor,
self.floor, self.floor, self.floor, self.floor, self.floor, self.floor,
])
# Collapse the wave to generate the level
generated_level = level_wave.collapse()
return generated_level