-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnpc.py
321 lines (271 loc) · 11.4 KB
/
npc.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#! /usr/bin/python
# NPC class
# Generic NPC class for basic storing of data,
# updating, and displaying
# Run this file to display random NPCs on screen
# Can move camera with up, down, left, and right keys
# npc.py is part of Sentience in Space.
#
# Sentience in Space is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Sentience in Space is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Sentience in Space. If not, see <http://www.gnu.org/licenses/>.
import binascii
import copy
import random
import pygame
from consts import *
class NPC:
def __init__(self, npc_id, pos_x, pos_y):
self.id = npc_id
self.name = ""
self.clip = 0 # which image to clip from sprite sheet; also which direction player is facing
self.count = 0 # Count the number of times update was called
# to limit updates per second
self.mode = 0 # 0 = not interacting, 1 =
self.say = 0 # dialogue index
self.font = pygame.font.Font(NPC_FONT_DIR, NPC_FONT_SIZE)
self.dialogue = [] # dialogue is a list, where each element is an array of
# [mission, preconditions, response flags
# postconditions, dialogue, (response flag, response dialogue),
# (response flag, response dialogue), ...]
#temporary code to make a placeholder dialogue
#preconditions and postconditions [self.alternate, self.spoken]
#responses are in order [response0, response1, ...]
#0=0, 1=1, 2=N/A
self.alternate = 0
self.spoken = 0
self.response = [0]
self.dialogue_index = 0
self.current_dialogue = []
self.current_response = []
#self.dialogue.append([1, [0, 2], [0], [1, 2], ["Hmm, his ID must be around here somewhere"]])
#self.dialogue.append([1, [1, 2], [0], [0, 2], ["I need to find his", "ID if it's around"]])
#self.dialogue.append([1, [2, 0], [0], [2, 1], ["Well, I give up. Maybe it'll turn up", "later. Guess I'll head on up to the bridge."]])
#self.dialogue.append([1, [2, 1], [0], [2, 2], ["I'll just look one more time..."]])
self.dialogue.append([1, [2, 2], [0], [2, 2], ["Anythin' I can do for ya?"], ([1], ["Do you know anything about Johannsen?"])])
self.dialogue.append([1, [2, 2], [1], [2, 2], ["He was feeding the cat last time I checked."]])
# print self.dialogue
#temp code stops here
# set the character position
self.x = pos_x
self.y = pos_y
# determine npc "type" based on its id
self.type = 0
self.sprite = pygame.image.load(NPC_SHEETS_DIR[self.type])
if self.sprite == None:
print "Error loading file: " + NPC_SHEETS_DIR[self.type]
self.sprite.set_colorkey(COLOR_KEY)
self.sprite = self.sprite.convert()
# Accessors and Modifiers
# should run this function before using NPC
def settype(self, new_type):
self.type = new_type
self.sprite = pygame.image.load(NPC_SHEETS_DIR[new_type])
if self.sprite == None:
return IMAGE_DOES_NOT_EXIST
return NO_PROBLEM
def gettype(self):
return self.type
def setname(self, name):
self.name = name
def getname(self):
return self.name
def setx(self, new_x):
self.x = new_x
def getx(self):
return self.x
def sety(self, new_y):
self.y = new_y
def gety(self):
return self.y
# set an initial NPC location
def spawn(self, x, y):
self.x = x
self.y = y
# start dialogue
def setdialogue(self, inventory, d):
self.say = d
# read in dialogue
def readdialogue(self):
dialogue_file = "dialogue" + self.id + ".txt"
f = open(dialogue_file, 'r') #open dialogue#.txt, where # is the ID of this NPC
line = f.readline()
while line != '':
splitline = string.split(line, ';')
if len(splitline) > 5:
#there are response dialogues
i = 6
for i in xrange(len(splitline)):
#group response dialogues together
response = (splitline[i], splitline[i+1])
del splitline[5:7]
splitline.insert(i, response)
temp = []
temp[0] = splitline[0]
temp[1] = string.split(splitline[1], ',')
temp[2] = string.split(splitline[2], ',')
temp[3] = string.split(splitline[3], ',')
i = 4
for i in xrange(len(splitline)):
temp[i] = splitline[i]
self.dialogue.append(temp)
line = f.readline()
#NPC talk, call when player talks to NPC
def rundialogue(self, mission):
self.say = mission
if mission == 0:
return ""
'''
#if in the middle of a response, do response dialogue
for i in xrange(len(self.dialogue)):
if self.dialogue[i][0] == mission:
for j in xrange(len(self.dialogue[i][2])):
if self.dialogue[i][2][j] == 1:
#now check if that response flag is met
if j == 0 and self.response[0] == 1:
#call functions to set postconditions
#construct tuple of NPC dialogue, Robot dialogue
#return the text
return self.dialogue[i][5]
'''
#if found no responses, find the appropriate dialogue
for i in xrange(len(self.dialogue)):
if self.dialogue[i][0] == mission:
bool_precon = True
for j in xrange(len(self.dialogue[i][1])):
if j == 0:
#checking alternate
if self.alternate != self.dialogue[i][1][j]:
bool_precon = False
if self.dialogue[i][1][j] == 2:
bool_precon = True
if j == 1:
#checking spoken
if self.spoken != self.dialogue[i][1][j]:
bool_precon = False
if self.dialogue[i][1][j] == 2:
bool_precon = True
for j in xrange(len(self.dialogue[i][2])):
if self.response[j] != self.dialogue[i][2][j]:
bool_precon = False
#if all conditions were true, call functions to set postconditions and return text
if bool_precon == True:
#set postconditions
#
self.dialogue_index = i
self.current_dialogue = self.dialogue[i][4]
if len(self.dialogue[self.dialogue_index]) >= 6:
return 1
return 0
#return self.dialogue[i][4]
#return "Yo_mamma's_face"
def responseoptions(self):
if len(self.dialogue[self.dialogue_index]) < 6:
return [""]
self.response = self.dialogue[self.dialogue_index][5][0]
return self.dialogue[self.dialogue_index][5][1]
# load from save string
def load(self, data):
self.type = ord(data[0])
self.settype(self.type)
name_len = int(binascii.hexlify(data[1:3]), 16)
data = data[3:]
self.name = data[:name_len]
data = data[name_len:]
self.x = ord(data[0])
self.y = ord(data[1])
self.clip = ord(data[2])
return NO_PROBLEM
# save NPC to a string of specified format
def save(self):
'''
Format:
type | name_len | name | x | y | clip
type - 1 byte
name_len - 2 bytes
name - name_len bytes
x - 1 byte
y - 1 byte
clip - 1 byte
'''
return chr(self.type) + binascii.unhexlify(makehex(len(self.name), 4)) + self.name + chr(self.x) + chr(self.y) + chr(self.clip)
def update(self, grid):
pass
# display NPC
def display(self, screen, camera):
if screen == None:
return SURFACE_DOES_NOT_EXIST
# if the NPC is out of camera focus
if (self.x < camera.x) or ((camera.x + camera.w) < self.x) or (self.y < camera.y) or ((camera.y + camera.h) < self.y):
return NOTHING_DONE
show = pygame.Rect((self.x - camera.x) * TILE_WIDTH, (self.y - camera.y) * TILE_HEIGHT, 0, 0)
clip = pygame.Rect(self.clip, 0, NPC_WIDTH, NPC_HEIGHT)
screen.blit(self.sprite, show, clip)
# if the NPC is talking
if self.say:
show = copy.deepcopy(NPC_TEXT_BOX)
show.y += 10
dy = self.font.size("")[1]
for line in self.current_dialogue: #self.rundialogue(self.say):
text = self.font.render(line, NPC_FONT_ANTIALIAS, NPC_FONT_COLOR)
screen.fill(WHITE, show)
screen.blit(text, show)
show.y += dy
for line in self.responseoptions():
text= self.font.render(line, NPC_FONT_ANTIALIAS, MEDIUM_SLATE_BLUE)
screen.fill(WHITE, show)
screen.blit(text, show)
show.y += dy
return NO_PROBLEM
if __name__=='__main__':
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) # create the screen
if screen == None:
sys.exit(SCREEN_DOES_NOT_EXIST)
pygame.display.set_caption("Character Demo")
camera = pygame.Rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)
# create NPCS
test = NPC(0, 0, 0)
# set dialogue for all of them
#for x in xrange(len(test)):
# test[x].setdialogue(1)
# test_dialogue = test[0].rundialogue(1)
# print test_dialogue
quit = False
while not(quit):
# single key presses
for event in pygame.event.get():
if event.type == pygame.QUIT: # exit when close window "X" is pressed
quit = True
if event.type == pygame.KEYDOWN:
if event.key == KEYBINDINGS[KB_UP]:
camera.y -= 1
elif event.key == KEYBINDINGS[KB_LEFT]:
camera.x -= 1
elif event.key == KEYBINDINGS[KB_DOWN]:
camera.y += 1
elif event.key == KEYBINDINGS[KB_RIGHT]:
camera.x += 1
if camera.x < 0:
camera.x = 0
if (camera.x + TILE_SHOW_W) > MAP_WIDTH:
camera.x = MAP_WIDTH - TILE_SHOW_W
if camera.y < 0:
camera.y = 0
if (camera.y + TILE_SHOW_H + 1) > MAP_HEIGHT:
camera.y = MAP_HEIGHT - TILE_SHOW_H - 1
screen.fill(BLACK)
test.update(None)
test.display(screen, camera)
pygame.display.flip()
pygame.time.Clock().tick(10)
pygame.quit()