-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgamestate.py
294 lines (256 loc) · 12.5 KB
/
gamestate.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import re
from character import Character
from pathlib import Path
import random
from preprocessor import Preprocessor
import sys
import time
from threading import Timer
import pygame
from pygame.locals import *
from renderer import DISPLAYSURF
from renderer import WINHEIGHT, WINWIDTH
from evaluate import evaluateSingleSample
# For textboxes
COLOR_INACTIVE = pygame.Color('lightskyblue3')
COLOR_ACTIVE = pygame.Color('dodgerblue2')
TEXT_SIZE = 25
textEdgeBufferW = 40
textEdgeBufferH = 20
backspacePressed = False
backspaceStart = time.time()
class OutputBox:
def __init__(self, x, y, w, h, text=''):
self.FONT = pygame.font.Font(None, TEXT_SIZE)
self.rect = pygame.Rect(x, y, w, h)
self.color = COLOR_INACTIVE
self.text = text
self.txt_surface = self.FONT.render(text, True, self.color)
self.active = False
def scrolling(self, args):
dialogue = args.dialogue
letter_i = args.letter_i
print(self.text)
self.text = dialogue[:letter_i]
# Re-render the text.
self.txt_surface = self.FONT.render(self.text, True, self.color)
# if len(self.text) < len(dialogue):
# t = Timer(0.5, self.scrolling(), args=(self, dialogue, letter_i+1)).start()
def handle_event(self, game_state, characterIndex, event):
# load the next dialogue text if available
# if event.type == pygame.KEYDOWN:
self.text = ""
# erase the text
pygame.draw.rect(DISPLAYSURF, (50,50,50), self.rect)
for letter in game_state.response_script[1]:
self.text = self.text + letter
# Re-render the text.
self.txt_surface = self.FONT.render(self.text, True, self.color)
# time.sleep(.1)
def update(self):
global backspaceStart
# Resize the box if the text is too long.
#if self.txt_surface.get_width() < screenWidth - textEdgeBufferW/2:
# width = max(200, self.txt_surface.get_width()+10)
# self.rect.w = width
def draw(self, screen):
pygame.draw.rect(screen, (50,50,50), self.rect)
# Blit the text.
screen.blit(self.txt_surface, (self.rect.x+5, self.rect.y+5))
# Blit the rect.
pygame.draw.rect(screen, self.color, self.rect, 2)
class InputBox:
def __init__(self, x, y, w, h, text=''):
self.FONT = pygame.font.Font(None, TEXT_SIZE)
self.rect = pygame.Rect(x, y, w, h)
self.color = COLOR_INACTIVE
self.text = text
self.txt_surface = self.FONT.render(text, True, self.color)
self.active = False
def handle_event(self, game_state, characterIndex, event):
global backspacePressed, backspaceStart
if event.type == pygame.MOUSEBUTTONDOWN:
# If the user clicked on the input_box rect.
if self.rect.collidepoint(event.pos):
# Toggle the active variable.
self.active = not self.active
else:
self.active = False
# Change the current color of the input box.
self.color = COLOR_ACTIVE if self.active else COLOR_INACTIVE
if event.type == pygame.KEYDOWN:
if self.active:
if event.key == pygame.K_RETURN:
game_state.response_script = {}
# here is where a character's eliza responds
game_state.response_script[1] = game_state.characters[characterIndex].eliza.respond(self.text)
if game_state.response_script[1] == "RUN_NEURAL_NETWORK":
game_state.response_script[1] = evaluateSingleSample(self.text, game_state.beam_size, game_state.encoder, game_state.decoder, game_state.voc)
game_state.response_script[1] = re.sub('\<EOS\>', '', game_state.response_script[1])
print(game_state.response_script[1])
self.text = ''
elif event.key == pygame.K_BACKSPACE:
self.text = self.text[:-1]
backspacePressed = True
backspaceStart = time.time()
else:
self.text += event.unicode
# Re-render the text.
self.txt_surface = self.FONT.render(self.text, True, self.color)
if event.type == pygame.KEYUP:
if self.active:
if event.key == pygame.K_BACKSPACE:
backspacePressed = False
def update(self):
global backspacePressed, backspaceStart
# Resize the box if the text is too long.
#if self.txt_surface.get_width() < screenWidth - textEdgeBufferW/2:
# width = max(200, self.txt_surface.get_width()+10)
# self.rect.w = width
#for debugging backspace
#print(str(backspacePressed)+" "+str(backspaceStart))
#delete characters when holding backspace down
if time.time() - backspaceStart > 0.35:
if backspacePressed:
backspaceStart += .05
self.text = self.text[:-1]
self.txt_surface = self.FONT.render(self.text, True, self.color)
def draw(self, screen):
pygame.draw.rect(screen, (50,50,50), self.rect)
# Blit the text.
screen.blit(self.txt_surface, (self.rect.x+5, self.rect.y+5))
# Blit the rect.
pygame.draw.rect(screen, self.color, self.rect, 2)
class GameState:
def __init__(self):
self.map = [
['W', 'W', 'W', 'W', 'W', 'W', 'W', 'W'],
['#', 'J', 'n', ' ', ' ', 'J', 'J', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', 'L', 'L', ' ', ' ', 'L', 'L', '#'],
['#', 'J', 'J', ' ', ' ', 'J', 'J', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', 'L', 'L', ' ', ' ', 'L', 'L', '#'],
['#', 'J', 'J', ' ', ' ', 'J', 'J', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', 'L', 'L', ' ', ' ', 'L', 'L', '#'],
['W', 'W', 'W', ' ', ' ', 'W', 'W', 'W'],
['#', 'J', 'J', ' ', ' ', 'J', 'J', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', 'L', 'L', ' ', ' ', 'L', 'L', '#'],
['#', 'J', 'J', ' ', ' ', 'J', 'J', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', 'L', 'm', ' ', ' ', 'L', 'L', '#'],
['#', 'J', 'J', ' ', ' ', 'J', 'J', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', 'L', 'L', ' ', ' ', 'L', 'L', '#'],
['W', 'W', 'W', ' ', ' ', 'W', 'W', 'W'],
['#', 'J', 'J', ' ', ' ', 'J', 'J', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', 'L', 'L', ' ', ' ', 'n', 'L', '#'],
['#', 'J', 'J', ' ', ' ', 'J', 'J', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', 'g', 'L', ' ', ' ', 'L', 'L', '#'],
['#', 'J', 'J', ' ', ' ', 'J', 'J', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', 'L', 't', ' ', ' ', 'L', 'L', '#'],
['W', 'W', 'W', ' ', ' ', 'W', 'W', 'W'],
['#', 'J', 'J', ' ', ' ', 'J', 'J', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', 'L', 'L', ' ', ' ', 'L', 'L', '#'],
['#', 'J', 'J', ' ', ' ', 'J', 'J', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', 'r', 'L', ' ', ' ', 'L', 'L', '#'],
['#', 'J', 'J', ' ', ' ', 'J', 'J', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', 'L', 'L', ' ', ' ', 'L', 'L', '#'],
['W', 'W', 'W', 'W', 'W', 'W', 'W', 'W']
]
self.player_pos = (len(self.map) // 2, len(self.map[0]) // 2)
self.cameraOffsetX = 0
self.cameraOffsetY = 0
self.currentImg = "down"
self.beam_size = None
self.encoder = None
self.decoder = None
self.voc = None
self.distance_to_bbX = 0
self.distance_to_bbY = 0
self.BOUNDING_BOX_RADIUS = 4
self.background_scroll_x = 0
self.currentDialogue = 1
self.response_script = {1:""}
output_box = OutputBox(textEdgeBufferW/2, WINHEIGHT - textEdgeBufferH - TEXT_SIZE*3, WINWIDTH - textEdgeBufferW, TEXT_SIZE*2)
input_box2 = InputBox(textEdgeBufferW/2, WINHEIGHT - textEdgeBufferH - TEXT_SIZE, WINWIDTH - textEdgeBufferW, TEXT_SIZE)
self.text_boxes = [output_box, input_box2]
self.characters = [
Character("Robby", "robot_avatar", "assets/robot_sprite.png", "scripts/characters/robot.txt"),
Character("Alicia", "girl_avatar", "assets/girl_sprite.png", "scripts/characters/girl.txt"),
Character("George", "monster_avatar", "assets/monster_sprite.png", "scripts/characters/monster.txt"),
Character("Alfred", "nervous_avatar", "assets/nervous_sprite.png", "scripts/characters/nervous.txt"),
Character("Mark", "trenchcoat_avatar", "assets/trenchcoat_sprite.png", "scripts/characters/trenchcoat.txt")
]
self.initCharacters()
@property
def outOfBBX(self):
return abs(self.distance_to_bbX) > self.BOUNDING_BOX_RADIUS
@property
def outOfBBY(self):
return abs(self.distance_to_bbY) > self.BOUNDING_BOX_RADIUS
def initCharacters(self):
# Randomly assign leadins and clues
# leadins
availableCharacters = list(range(len(self.characters)))
for filename in Path("scripts/leadins").glob("**/*.txt"):
characterIndex = int((random.random() * len(availableCharacters)) // 1)
print(characterIndex)
with open(filename) as leadin_script:
content = leadin_script.read()
self.characters[characterIndex].eliza.combined_script += content
if len(availableCharacters) == 0:
break
if characterIndex in availableCharacters:
availableCharacters.remove(characterIndex)
# clues
def randomIndex():
return int((random.random() * len(self.characters)) // 1)
pre = Preprocessor(self.characters)
# Assign killer
killerIndex = randomIndex()
# bags
helped = randomIndex()
helpwitness = randomIndex()
self.characters[helped].eliza.combined_script += pre.replace('scripts/clues/bags1.txt',{'helped':helped,'killer':killerIndex})
self.characters[helpwitness].eliza.combined_script += pre.replace('scripts/clues/bags2.txt',{'helped':helped,'helpwitness':helpwitness,'killer':killerIndex})
# scared
scared = randomIndex()
scared_witness = randomIndex()
self.characters[scared].eliza.combined_script += pre.replace('scripts/clues/scared1.txt',{'scared':scared,'killer':killerIndex})
self.characters[scared_witness].eliza.combined_script += pre.replace('scripts/clues/scared2.txt',{'scared':scared,'scared_witness':scared_witness,'killer':killerIndex})
# lunch
lunch = randomIndex()
self.characters[lunch].eliza.combined_script += pre.replace('scripts/clues/lunch.txt',{'lunch':lunch,'killer':killerIndex})
# bathroom
bathroom = randomIndex()
bathroom_witness = randomIndex()
sick_witness = randomIndex()
self.characters[bathroom_witness].eliza.combined_script += pre.replace('scripts/clues/bathroom1.txt',{'bathroom':bathroom,'bathroom_witness':bathroom_witness,'killer':killerIndex})
self.characters[sick_witness].eliza.combined_script += pre.replace('scripts/clues/bathroom2.txt',{'bathroom':bathroom,'sick_witness':sick_witness,'killer':killerIndex})
# untrustworthy
suspicious = randomIndex()
suspect = randomIndex()
self.characters[suspicious].eliza.combined_script += pre.replace('scripts/clues/untrustworthy.txt',{'suspicious':suspicious,'suspect':suspect,'killer':killerIndex})
for character in self.characters:
character.load()