-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNPC.py
More file actions
358 lines (326 loc) · 16.4 KB
/
Copy pathNPC.py
File metadata and controls
358 lines (326 loc) · 16.4 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import pygame, Constants, DObject, Wall, math, json, Item, AI, OnScreenMenu, random
from pygame.locals import *
from Constants import *
from DObject import *
WALKSPEED = 3 #6.5 for normal, 10 for charge
WALKACCEL = .3 #.7 for normal, .45 for charge
MAXRUNSPEED = 4 # 7.5 for normal modes, 13 for charge
RUNACCEL = 1.5 #3.5 for normal2.5for charge
RUNDECEL = 2.5 #5.5 for normal,3.5for charge
FRICTION = .3 #percent of horizontal acceleration to remove. .2 for normal, .08 for charge
SHORTHOP = -18 #-30
JUMPEXTENSION = -6 #-.8
MAXFALLSPEED = 15
MAXRISESPEED = -20 #-30
AIRRUNACCEL = 1.0 #2.2 for normal .8 for charge
MAXAIRSPEED = 6 #10 for normal modes, 15 for charge
AIRFRICTION = .02 #.02 for normal, .01 for charge
DEFAULT_HEALTH = 20
DEFAULTTEXT = [[["What's up Mai?","We've always got so much work to do."],["I wish we could go back to the old days.","I used to have time to tinker with fun things."," "]],[["Unless there's an elephant on fire in the hallway,","...","Don't bother me."],["..."],["...."],["......."],["........."],["Pls go do your own work."]],[["How's it feel being head engineer?"]],[["I wonder where Arthur went.","I can't find him."," "]],[["I read it once in a history book that American teenagers used to use","'Yolo' as an excuse to do stupid things."],["It's quite a miracle we're alive,","Given how badly they've fucked the world over."]],[["Sorry Mai, I've got to go to the Director's office.","See you soon though."," "]],[["I'm still distraught.","My family....","..."]],[["I hear the Director isn't doing so well.","His age is starting to take it's toll."," "]],[["When's the last time we checked the solar panels?","I heard there was a power degradation as of late."," "]],[["Hey Mai!"],["Do you know when the new shifts will be posted?"],[" ","..."],[" ","Oh, so you don't know either. "," "]],[["This low gravity is supposed to help us get around,","but honestly I wish it was higher."," "]],[["It's still hard to believe."],["All the world's old internet content.","...","Gone."],["Since the incident, no one has been running the servers.","Only automated systems are online,","and even those aren't being fixed when they break."],["I remember watching Markiplier youtube videos as a college student,","but now our net is so limited,","not even youtube is online."," "]]]
DEFAULTNPC = ['CM','SF','EF','BM']
class NPC(DObject):
def __init__(self,x,y,parsurf=[None],prefix = None,content = None,parent = None,isfoc = False):
DObject.__init__(self,x,y,parentsurface = parsurf)
self.parent = parent
if content == None:
self.content = DEFAULTTEXT[random.randint(0,len(DEFAULTTEXT)-1)]
else:
self.content= content
if prefix == None:
prefix = DEFAULTNPC[random.randint(0,len(DEFAULTNPC)-1)]
self.imglst = []
self.ACCEL = WALKACCEL
self.SPEED = WALKSPEED
#~~~~~~~ movement attributes
self.dx=0.0
self.dy=0.0
self.ddx = 0.0
self.ddy = 0.0
self.ox, self.oy=x,y
self.x = float(x)
self.y = float(y)
self.prevpos = [x,y]
#~~~~~~~ contact flags
self.contact = False
self.wasonground = False
self.grounded = False
self.direction = 0
self.crouching = False
self.crouchcount = 0
self.prevstate = None
self.health = 20.0
self.defense = 3.0
self.power = 5
self.object = None
self.radius = 30
self.jump = self.doublejump = False
#self.health = Constants.
self.JumpLeeWay = 0 #number of frames we were airborn
#~~~~~~~ image/draw material + collision rect definition
#imglst holds a dictionary of all the available sprites and animations
#State represents animation state, not true states.
self.MakeImglst(prefix)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
self.frame = 0 #the image itself to use in the current state
self.state = 0 #idle right. the integer represents the animation states all states are divisible by 2. odds are left facing
self.image = self.imglst[(self.state + self.direction)%len(self.imglst)][self.frame]
self.rect = Rect(0,0,PLW,PLH)
self.rect.midbottom = [self.x,self.y]
self.collisionrect = Rect(x,y,36,52)
self.collisionrect.midbottom = [self.x,self.y]
#~~~~~ other features
self.collideswith = None
self.triggerswith = None
self.inputs = PS3.copy()
self.PrevInputs = self.inputs.copy()
self.PumpInput();self.PumpInput()#<set all to 0.0
self.groundobj = Wall.Wall(x,y)
self.IsFocus = isfoc
self.AI = AI.AI_Run_Tortadillo
self.box = OnScreenMenu.TextBox(None,self.content,20,Constants.SCREEN.get_height()-140,Constants.SCREEN.get_width()-40,120)
self.ActionCount = 0
self.ResumeActionCount = random.randint(FPS,FPS*3)
self.PauseCount = random.randint(FPS,FPS*3)
self.triggerframe = 0
self.reqtag = self.prereq = self.gentag = self.effect = None
self.story = 'Main'
self.TRIGGED = self.Reload = False
def MakeImglst(self, prefix = 'BM'):
self.imglst = []
#self.imglst.append(MIL2("objects/Laser/LaserCamFire*.png",2)[0])
#self.imglst.append(MIL2("objects/Laser/LaserCamFire*.png",2)[1])
self.imglst.append(MIL2("objects/NPC/"+str(prefix)+"Idle*.png",5)[1])
self.imglst.append(MIL2("objects/NPC/"+str(prefix)+"Idle*.png",5)[0])
self.imglst.append(MIL2("objects/NPC/"+str(prefix)+"Walk*.png",5)[1])
self.imglst.append(MIL2("objects/NPC/"+str(prefix)+"Walk*.png",5)[0])
def changeState(self,num):
'''num must be divisible by 2'''
#print "changestate state: "+str(self.state+self.direction)+" num: "+str(num)
if (self.state+self.direction)/2 == num/2:
return False
self.state = num%4
self.frame = 0
def HorizMotion(self, amnt, useinputs = False):
'''use this instead of setting it every instance'''
self.ddx += amnt
if self.grounded:
frict = self.ddx*FRICTION*self.groundobj.tract
if abs(self.ddx) < self.SPEED and useinputs:
self.ddx += self.inputs[COM['Move']]*self.ACCEL*self.groundobj.tract
self.ddx -= frict
else:
frict = self.ddx*AIRFRICTION
if abs(self.ddx) < MAXAIRSPEED and useinputs:
self.ddx += self.inputs[COM['Move']]*AIRRUNACCEL
self.ddx -= frict
def AdjustState(self):
'''would use self.inputs to determine state. changes states, setting previous state to the one the frame before'''
cur_frame = 0
self.frame += 1
self.prevstate = self.state
self.ddy += GRAV
self.prevpos = [self.collisionrect.centerx,self.collisionrect.centery]
if self.grounded and not self.wasonground and self.JumpLeeWay > 4:
self.changeState(0) #no land animation
if self.grounded:
self.JumpLeeWay = 0
elif not self.grounded:
#not grounded
if self.wasonground:
self.collisionrect.bottom+= self.ddy
else:
self.JumpLeeWay += 1
if self.ddy >= GRAV and (self.JumpLeeWay > 4):
self.changeState(6) #fall animation
#adjust x acceleration first
#can fix positive Y accel to add gravity
if self.grounded:
self.ddx -= FRICTION*self.ddx
else:
self.ddx -= self.ddx*AIRFRICTION
if self.ddy > MAXFALLSPEED or self.ddy < MAXRISESPEED:
self.ddy -= cmp(self.ddy,0)*GRAV
self.crouching = False
#if we have attempted movement in the x axis
if self.inputs[COM['Move']] != 0:
self.direction = int(cmp(0,self.inputs[COM['Move']])/2.0 + .5) # facing
#add some acceleration to the left each frame
if self.grounded:
if abs(self.ddx) < self.SPEED:
self.ddx += self.ACCEL*self.inputs[COM['Move']]*self.groundobj.tract + self.ddx*FRICTION
elif cmp(self.ddx,0) != cmp(self.inputs[COM['Move']],0):
self.ddx += self.ACCEL*self.inputs[COM['Move']]*self.groundobj.tract
self.changeState(2)
elif not self.grounded:
if abs(self.ddx) < MAXAIRSPEED:
self.ddx += AIRRUNACCEL*self.inputs[COM['Move']]
else:
self.ddx -= self.ddx*AIRFRICTION
elif self.inputs[COM['Move']] == 0:
if self.grounded:
self.changeState(0)
else:
self.ddx -= self.ddx*AIRFRICTION
if self.wasonground and not self.grounded:
pass #TODO, do we need to change states?
if self.state/2 in [0,1,2,3,4,5]:
self.frame = self.frame%len(self.imglst[(self.state+self.direction)%len(self.imglst)])
elif self.frame/len(self.imglst[(self.state+self.direction)%len(self.imglst)]):
self.frame = 0
num = 0
if self.state/2 in [3,9]:
num = 0
elif self.state/2 in [1,6]:
num = self.state+2
self.changeState(num)
self.wasonground = self.grounded
self.grounded = False
def touchground(self,touchedobj):
self.grounded = True
self.contact = True
self.groundobj = touchedobj
def Draw(self,surf, relrect = None):
LELx = self.groundobj.rect.x - self.rect.x
LELy = self.groundobj.rect.y - self.rect.y
if relrect == None:
#we weren't given a location to defer to
surf.blit(self.image,self.rect)
else:
surf.blit(self.image,relrect)
#pygame.draw.rect(surf,(200,200,100),(relrect.left+LELx,relrect.top+LELy,TLWDTH,TLHGHT),4)
def setCollideables(self,spritegroups):
self.collideswith = spritegroups
def setTriggers(self,list_of_objects):
self.triggerswith = list_of_objects
def addTrigger(self,DObjectObj):
self.triggerswith.append(DObjectObj)
def addCollideable(self,DObjectObj):
self.collideswith.append(DObjectObj)
def damage(self,obj,amount,stun,subtype):
if subtype in "physical":
return
else:
self.health -= amount
print "NPC health is: "+str(self.health)
if self.health <= 0:
if self in self.parent.all_updates:
self.parent.all_updates.remove(self)
for i in self.parent.all_layers:
if self in i:
i.remove(self)
def update(self):
'''Use PumpInput before calling this method, otherwise there will be no user input. When the game is paused, update should never be called.'''
o = self.parent.User
dx = o.rect.centerx - self.rect.centerx
dy = o.rect.bottom - self.rect.bottom
d = dx*dx+dy*dy
d = math.sqrt(d)
if (self.object != None and self.triggerframe > 0):
#TODO generate text box
self.object.changeState(0+self.object.direction)
self.object.image = self.object.imglst[self.object.direction][self.object.frame].convert_alpha()
self.object.groundobj.PushToGround(self.object)
self.object.rect.midbottom = self.object.collisionrect.midbottom
dx = self.object.rect.centerx - self.rect.centerx
self.box.Reload = self.Reload
self.box.Activate(Constants.CUR_LEVEL)
self.object = None
self.triggerframe = 0
if self.TRIGGED:
return
if self.prereq != None:
for tag in self.prereq:
if tag not in Constants.P_STORY_PROGRESSION[self.story]:
return
if 'NPC_'+str(self.ox)+str(self.oy)+Constants.CUR_LEVEL.filename not in Constants.P_STORY_PROGRESSION[self.story]:
Constants.P_STORY_PROGRESSION[self.story].append('NPC_'+str(self.ox)+str(self.oy)+Constants.CUR_LEVEL.filename)
else:
if 'NPC_'+str(self.ox)+str(self.oy)+Constants.CUR_LEVEL.filename not in Constants.P_STORY_PROGRESSION[self.story]:
Constants.P_STORY_PROGRESSION[self.story].append('NPC_'+str(self.ox)+str(self.oy)+Constants.CUR_LEVEL.filename)
#after adding self, check if required tags are here, if so, increment story.
if self.reqtag != None:
for i in self.reqtag:
if i not in Constants.P_STORY_PROGRESSION[self.story]:
return
Constants.P_STORY_PROGRESSION[self.story][0] += 1
if self.effect != None:
self.effect()
print "upped story int to: "+str(Constants.P_STORY_PROGRESSION[self.story][0])
self.TRIGGED = True
if self.gentag != None and self.gentag not in Constants.P_STORY_PROGRESSION:
Constants.P_STORY_PROGRESSION[self.gentag] = {0}
if d <= self.radius:
self.trigger(o)
if self.triggerframe !=0:
self.triggerframe += 1
if self.PauseCount > 0:
self.PauseCount -= 1
self.state = 0
self.frame = (self.frame+1)%len(self.imglst[self.state])
self.image = self.imglst[(self.state+self.direction)%len(self.imglst)][self.frame].convert_alpha()
self.rect = self.image.get_rect()
self.rect.midbottom = self.collisionrect.midbottom
return
self.ActionCount += 1
if not self.IsFocus and self.ActionCount <= self.ResumeActionCount:
self.AI(self)
elif self.ActionCount > self.ResumeActionCount:
self.ActionCount = 0
self.ResumeActionCount = random.randint(FPS,FPS*3)
self.direction = random.randint(0,2)
self.PauseCount = random.randint(FPS,FPS*3)
self.dy = .01
self.dx = 0
self.AdjustState()
self.image = self.imglst[(self.state+self.direction)%len(self.imglst)][self.frame].convert_alpha()
self.rect = self.image.get_rect()
self.rect.midbottom = self.collisionrect.midbottom
#physics portion
self.dx+= self.ddx
self.collisionrect.right+=math.floor(self.dx+.5)
self.collide(self.dx,0)
#now test for contact
self.dy += self.ddy
self.collisionrect.bottom += self.dy
self.collide(0, self.dy)
#set image rect data
#self.collisionrect.midbottom = (self.x,self.y)
self.x = self.collisionrect.centerx
self.y = self.collisionrect.centery
if self.ActionCount == 0:
self.collisionrect.centerx = self.prevpos[0]
def trigger(self,o):
'''as a door, go to new map or elsewhere on map'''
if (str(type(o))=="<class 'Player.Player'>"):
if o.crouchcount == 1 and self.object == None:
self.object = o
self.triggerframe = 1
def collide(self, movx, movy):
for o in self.parent.world:
if not o.CanCollide:
continue
if self.collisionrect.colliderect(o):
if ('Wall.Wal' in str(type(o))) or ('Wall.Pla' in str(type(o))):
o.act(self,movx,movy)
else:
print "NPC failed. no non-wall objects should be around."
for o in self.parent.updates:
if not o.CanCollide or o == self:
continue
if self.collisionrect.colliderect(o):
if (str(type(o))=="<class 'Player.Player'>"):
o.act(self,movx,movy)
def PumpInput(self,commands={None}):
'''store commands for use in the update method.
check Constants.py to understand the format
stores relevant info in children'''
self.PrevInputs = self.inputs.copy()
for i in self.inputs:
self.inputs[i]= 0.0
for element in commands:
if element in self.inputs:
self.inputs[element] = commands[element]
def act(self, obj, dx,dy):
'''default collision between us and an object.'''
if 'Player' in str(type(obj)):
obj.damage(self,self.power)