This repository has been archived by the owner on Sep 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathw_i_stage.py
303 lines (253 loc) · 9.54 KB
/
w_i_stage.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
import abc
from panda3d.core import (
CollisionHandlerQueue,
CollisionRay,
CollideMask,
CollisionHandlerPusher,
CollisionNode,
CollisionSphere,
CollisionBox)
from direct.actor.Actor import Actor
from panda3d.ai import AICharacter
from w_gui_dialog import GUIDialog
from w_rpg_field_actor import FieldActor
from y_better_dice import BetterThanDice
from w_rpg_field_grid import StageGrid
class IStage(metaclass=abc.ABCMeta):
def __init__(self):
self.charHeading = {"up": 180, "down": 0, "left": 90, "right": 90}
self.pusher = base.pusher
self.heroGroundHandler = CollisionHandlerQueue()
self.dialogBox = GUIDialog()
self.stage = None
self.hero = None
self.enemyActors = []
self.npcActors = []
self.bossActors = []
def initStage(self):
if self.stage:
self.stage.removeNode()
this_map = base.mapData.maps[base.gameData.currentMap]
self.stage = loader.loadModel(this_map['model'])
if "pattern" in this_map:
self.stage_grid = StageGrid(
this_map['pattern'], self.stage, this_map['blockTypes'])
self.startPos = self.stage_grid.startPosList[base.gameData.heroPos]
else:
self.startPos = self.stage.find(
"**/" + base.gameData.heroPos).getPos()
self.stage.reparentTo(render)
self.stage.hide()
def setup(self):
self.initStage()
self.initHero()
self.initEnemy()
self.initBoss()
self.initNPC()
def initHero(self):
self.hero = loader.loadModel("charaRoot")
self.hero.reparentTo(self.stage)
model = base.gameData.heroModel
self.heroArmature = Actor(
"{}".format(model), {
"idle": "{}-idle".format(model),
"walk": "{}-run".format(model)
})
self.heroArmature.reparentTo(self.hero)
self.hero.setPos(self.startPos)
cNode = CollisionNode('hero')
cNode.addSolid(CollisionSphere(0, 0, 1.5, 1))
heroCollision = self.hero.attachNewNode(cNode)
#########################################################
# heroCollision.show()
self.pusher.addCollider(
heroCollision, self.hero, base.drive.node())
base.cTrav.addCollider(heroCollision, self.pusher)
heroGroundRay = CollisionRay()
heroGroundRay.setOrigin(0, 0, 9)
heroGroundRay.setDirection(0, 0, -1)
heroGroundCol = CollisionNode('heroRay')
heroGroundCol.addSolid(heroGroundRay)
heroGroundCol.setFromCollideMask(CollideMask.bit(0))
heroGroundCol.setIntoCollideMask(CollideMask.allOff())
heroGroundColNp = self.hero.attachNewNode(heroGroundCol)
#########################################################
# heroGroundColNp.show()
base.cTrav.addCollider(heroGroundColNp, self.heroGroundHandler)
self.controlCamera()
def fixActorsHPR(self):
for e in self.enemyActors:
if not e:
continue
h, p, r = e.actor.getHpr()
e.actor.setHpr(h, 0, 0)
def AIUpdate(self, task):
base.AIworld.update()
self.fixActorsHPR()
return task.cont
def initEnemy(self):
this_map = base.mapData.maps[base.gameData.currentMap]
if not "enemyList" in this_map:
return
enemyNumber = len(this_map["enemyList"])
if enemyNumber <= 0:
return
self.enemyActors = []
die = BetterThanDice(2)
for enemyIndex in list(range(0, enemyNumber)):
enemyModel = this_map["enemyList"][enemyIndex]['model']
fa = FieldActor('enemy', enemyIndex, enemyModel,
self.stage, self.enemyActors, self.hero)
if die.getValue() == 0:
fa.request('Pursue')
else:
fa.request('Wander')
def initBoss(self):
this_map = base.mapData.maps[base.gameData.currentMap]
if not "bossList" in this_map:
return
bossNumber = len(this_map["bossList"])
if bossNumber <= 0:
return
self.bossActors = []
for bossIndex in list(range(0, bossNumber)):
if not base.gameData.flags[this_map["bossList"][bossIndex]['flag']]:
continue
bossModel = this_map["bossList"][bossIndex]['model']
fa = FieldActor('boss', bossIndex, bossModel,
self.stage, self.bossActors, self.hero)
def initNPC(self):
this_map = base.mapData.maps[base.gameData.currentMap]
if not "npcList" in this_map:
return
npcNumber = len(this_map["npcList"])
if npcNumber == 0:
return
self.npcActors = []
for npcIndex in list(range(0, npcNumber)):
npcModel = this_map["npcList"][npcIndex]['model']
fa = FieldActor('npc', npcIndex, npcModel,
self.stage, self.npcActors, self.hero)
fa.request('Idle')
def controlCamera(self):
base.camera.setPos(self.hero.getX(), self.hero.getY() - 50, 55)
base.camera.lookAt(self.heroArmature)
base.sunNp.setPos(self.hero.getX()-4, self.hero.getY()-4, 50)
base.sunNp.lookAt(self.hero)
def moveHero(self, task):
animName = self.heroArmature.getCurrentAnim()
if self.dialogBox.isShowing:
if animName != "idle":
self.heroArmature.loop("idle")
self.controlDialogBox()
return task.cont
keysPressed = sum(base.directionMap.values())
if keysPressed == 0:
if animName != "idle":
self.heroArmature.loop("idle")
if base.commandMap["confirm"]:
pass
elif base.commandMap["cancel"]:
self.cancelCommand()
base.resetButtons()
return task.cont
self.heroHandleFloor()
self.heroCalculateDisplacement()
self.heroHeading(keysPressed)
self.controlCamera()
if animName != "walk":
self.heroArmature.loop("walk")
return task.cont
def heroCalculateDisplacement(self):
dt = globalClock.getDt()
xSpeed = 0.0
ySpeed = 0.0
speed = 25
if base.directionMap["left"]:
xSpeed -= speed
elif base.directionMap["right"]:
xSpeed += speed
if base.directionMap["up"]:
ySpeed += speed
elif base.directionMap["down"]:
ySpeed -= speed
self.hero.setX(self.hero, xSpeed * dt)
self.hero.setY(self.hero, ySpeed * dt)
def heroHandleFloor(self):
entries = list(self.heroGroundHandler.getEntries())
entries.sort(key=lambda x: x.getSurfacePoint(render).getZ())
if len(entries) > 0 and entries[0].getIntoNode().getName() == "floor":
self.hero.setZ(entries[0].getSurfacePoint(render).getZ())
def controlDialogBox(self):
if self.dialogBox.isChoices:
pass
else:
if base.commandMap["confirm"] or base.commandMap["cancel"]:
self.dialogBox.next()
base.resetButtons()
def heroHeading(self, keysPressed=None):
if not keysPressed:
return
heading, pitch, roll = self.heroArmature.getHpr()
sumAngles = 0
for key, value in base.directionMap.items():
if value:
sumAngles += self.charHeading[key]
direction = -1 if base.directionMap["left"] else 1
heading = (sumAngles * direction) / keysPressed
self.heroArmature.setHpr(heading, pitch, roll)
def setHeroCollision(self):
inEvent = "{}-into-{}".format('hero', 'world')
self.pusher.addInPattern(inEvent)
base.accept(inEvent, self.intoEvent)
# againEvent = "{}-again-{}".format(enemyColName, heroColName)
# self.pusher.addAgainPattern(againEvent)
# base.accept(againEvent, self.printAgain)
# outEvent = "{}-out-{}".format(enemyColName, heroColName)
# self.pusher.addOutPattern(outEvent)
# base.accept(outEvent, self.printOut)
def changeMap(self):
base.callLoadingScreen()
self.resetMap()
def resetMap(self):
self.setup()
self.start()
base.initController()
def start(self):
render.setLight(base.alnp)
render.setLight(base.sunNp)
self.controlCamera()
self.stage.show()
self.hero.show()
for enemy in self.enemyActors:
if enemy:
enemy.request('Show')
for boss in self.bossActors:
if boss:
boss.request('Show')
for npc in self.npcActors:
npc.request('Show')
# change how to play different music later
base.messenger.send("playMap")
base.removeLoadingScreen()
def stop(self):
self.stage.hide()
self.hero.hide()
for enemy in self.enemyActors:
if enemy:
enemy.request('Hide')
for boss in self.bossActors:
if boss:
boss.request('Hide')
for npc in self.npcActors:
npc.request('Hide')
render.clearLight()
def quit(self):
self.stage.removeNode()
self.dialogBox.removeNode()
@abc.abstractmethod
def cancelCommand(self):
raise NotImplementedError('subclass must define this method')
@abc.abstractmethod
def intoEvent(self, entry):
raise NotImplementedError('subclass must define this method')