-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscene.py
208 lines (172 loc) · 7.07 KB
/
scene.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
"""
scene.py --
Copyright (c) 2007 Sean Hammond [email protected]
This file is part of PandaSteer.
PandaSteer 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 2 of the License, or
(at your option) any later version.
PandaSteer 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 PandaSteer; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
# Python imports
import math,sys,random,os
# Panda3D imports
from pandac.PandaModules import Vec2,Vec3,Vec3D,DirectionalLight,VBase4,PointLight
from direct.showbase.DirectObject import DirectObject
from pandac.PandaModules import TransparencyAttrib
from direct.gui.DirectGui import *
from pandac.PandaModules import TextNode
from pandac.PandaModules import AmbientLight,Spotlight,PerspectiveLens,Fog
from pandac import PandaModules as P
from direct.task.Task import Task
# Custom imports
from camera import Camera,EdgeScreenTracker
from terrain import Terrain
from obstacles import SphereObstacle
from containers import ContainerSquare
from steerVec import SteerVec
import character as C
# CollisionTraverser for Plants
base.cTrav = P.CollisionTraverser('CollisionTraverser of scene.py')
plantNode = P.NodePath('plantNode') # All plants are parented to one node for flattening
plantNode.reparentTo(render)
class Sea:
def __init__(self):
sea = loader.loadModel("models/sea1.egg")
sea.reparentTo(render)
sea.setScale(2000,2000,100)
class Scene(DirectObject):
def __init__(self):
"""Initialise the scene."""
# Show the framerate
base.setFrameRateMeter(True)
# Initialise terrain:
# Make 4 terrain nodepath objects with different hilliness values
# and arrange them side-by-side in a 2x2 grid, giving a big terrain
# with variable hilly and flat areas.
color = (0.6,0.8,0.5,1) # Bright green-ish
scale = 12
height = 18 # FIXME: For now we are raising the terrain so it
# floats above the sea to prevent lakes from
# appearing (but you still get them sometimes)
t1 = Terrain(color=color,scale=scale,trees=0.7,pos=P.Point3(0,0,height))
t1.prime.reparentTo(render)
t2 = Terrain(color=color,scale=scale,h=24,pos=P.Point3(32*scale,0,height),trees=0.5)
t2.prime.reparentTo(render)
t3 = Terrain(color=color,scale=scale,h=16,pos=P.Point3(32*scale,32*scale,height),trees=0.3)
t3.prime.reparentTo(render)
t4 = Terrain(color=color,scale=scale,h=2,pos=P.Point3(0,32*scale,height),trees=0.9)
t4.prime.reparentTo(render)
#tnp1.setPos(tnp1,-32,-32,terrainHeight)
# Initialise sea
sea = Sea()
# Initialise skybox.
self.box = loader.loadModel("models/skybox/space_sky_box.x")
self.box.setScale(6)
self.box.reparentTo(render)
# Initialise characters
self.characters = []
self.player = C.Character(model='models/eve',run='models/eve-run',
walk='models/eve-walk')
self.player.prime.setZ(100)
self.player._pos = SteerVec(32*12+random.random()*100,32*12+random.random()*100)
self.player.maxforce = 0.4
self.player.maxspeed = 0.55
EdgeScreenTracker(self.player.prime,dist=200) # Setup camera
for i in range(0,11):
self.characters.append(C.Character())
self.characters[i].prime.setZ(100)
self.characters[i].wander()
self.characters[i].maxforce = 0.3
self.characters[i].maxspeed = 0.2
self.characters[i]._pos = SteerVec(32*12+random.random()*100,32*12+random.random()*100)
C.setContainer(ContainerSquare(pos=SteerVec(32*12,32*12),radius=31*12))
#C.toggleAnnotation()
# Initialise keyboard controls.
self.accept("c",C.toggleAnnotation)
self.accept("escape", sys.exit)
# Setup CollisionRay and CollisionHandlerQueue for mouse picking.
self.pickerQ = P.CollisionHandlerQueue()
self.picker = camera.attachNewNode(P.CollisionNode('Picker CollisionNode'))
self.picker.node().addSolid(P.CollisionRay())
# We want the picker ray to collide with the floor and nothing else.
self.picker.node().setFromCollideMask(C.floorMASK)
self.picker.setCollideMask(P.BitMask32.allOff())
base.cTrav.addCollider(self.picker,self.pickerQ)
try:
handler.addCollider(self.picker,camera)
except:
pass
self.accept('mouse1',self.onClick)
# Set the far clipping plane to be far enough away that we can see the
# skybox.
base.camLens.setFar(10000)
# Initialise lighting
self.alight = AmbientLight('alight')
self.alight.setColor(VBase4(0.35, 0.35, 0.35, 1))
self.alnp = render.attachNewNode(self.alight)
render.setLight(self.alnp)
self.dlight = DirectionalLight('dlight')
self.dlight.setColor(VBase4(0.4, 0.4, 0.4, 1))
self.dlnp = render.attachNewNode(self.dlight)
self.dlnp.setHpr(45, -45, 0)
render.setLight(self.dlnp)
self.plight = PointLight('plight')
self.plight.setColor(VBase4(0.8, 0.8, 0.5, 1))
self.plnp = render.attachNewNode(self.plight)
self.plnp.setPos(160, 160, 50)
self.slight = Spotlight('slight')
self.slight.setColor(VBase4(1, 1, 1, 1))
lens = PerspectiveLens()
self.slight.setLens(lens)
self.slnp = render.attachNewNode(self.slight)
self.slnp.setPos(-20, -20, 20)
self.slnp.lookAt(50,50,0)
# Initialise some scene-wide exponential fog
colour = (0.5,0.8,0.8)
self.expfog = Fog("Scene-wide exponential Fog object")
self.expfog.setColor(*colour)
self.expfog.setExpDensity(0.0005)
render.setFog(self.expfog)
base.setBackgroundColor(*colour)
# Add a task for this Plant to the global task manager.
self.stepcount = 0
taskMgr.add(self.step,"Plant step task")
def step(self,task):
if self.stepcount < 3:
self.stepcount+=1
return Task.cont
else:
plantNode.flattenStrong()
print render.analyze()
return Task.done
def onClick(self):
"""Handle the mouse-click event."""
mpos=base.mouseWatcherNode.getMouse()
# Makes the ray's origin the camera and make the ray point to mpos
###self.picker.node().getSolid(0).setFromLens(
###base.camNode,mpos.getX(),mpos.getY())
##zuck
self.picker.node().modifySolid(0).setFromLens(
base.camNode,mpos.getX(),mpos.getY()
)
# We don't want to traverse now, so wait for panda to do it then move.
taskMgr.doMethodLater(.02,self.setDestination,'setDest')
def setDestination(self,task):
"""Helper method for onClick.
Find the position in the 3D scene that was clicked and pass it to the
click method of the currently active plugin.
"""
if self.pickerQ.getNumEntries() > 0:
self.pickerQ.sortEntries()
self.point=self.pickerQ.getEntry(0).getSurfacePoint(render)
self.player.arrive(SteerVec(self.point.getX(),self.point.getY()))
# Run the test scene
s = Scene()
run()