-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcrystalobject.py
More file actions
73 lines (56 loc) · 2.46 KB
/
crystalobject.py
File metadata and controls
73 lines (56 loc) · 2.46 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
from direct.interval.MetaInterval import Sequence
from direct.showbase.DirectObject import DirectObject
from panda3d.bullet import BulletConvexHullShape, BulletRigidBodyNode
from panda3d.core import Vec3, LPoint3, NodePath
class CrystalObject(DirectObject):
def __init__(self, position=Vec3(0, 0, 0), model='models/box.egg', scale=Vec3(0.25, 0.25, 0.25),
name='default'):
DirectObject.__init__(self)
self.model = base.loader.loadModel(model)
self.model.setScale(scale.x, scale.y, scale.z)
self.model.setPos(-self.model.getBounds().getCenter())
self.model.setTwoSided(False, 1)
self.model.flattenLight()
self.model.clear_model_nodes()
geom = self.model.findAllMatches('**/+GeomNode')[0].node().getGeom(0)
shape = BulletConvexHullShape()
shape.addGeom(geom)
node = BulletRigidBodyNode(name)
node.addShape(shape)
node.setMass(0.01)
self.np = base.render.attachNewNode(node)
self.np.setPos(position)
base.world.attachRigidBody(node)
crystalRotate = self.model.hprInterval(10, LPoint3(360, 0, 0))
crystalUp = self.model.posInterval(1, LPoint3(0, 0, 1))
crystalDown = self.model.posInterval(1, LPoint3(0, 0, 0))
crystalMovement = Sequence(crystalUp, crystalDown)
crystalRotate.loop()
crystalMovement.loop()
self.model.reparentTo(self.np)
self.playerNode = base.render.findAllMatches("**/*Player")[0]
self.maxSpeed = 30
self.lifetime = 500
self.add_task(self.track_lifetime, 'track_crystal')
self.add_task(self.move_to_player, 'go_to_player')
def track_lifetime(self, task):
self.lifetime -= 0.05
if self.lifetime < 0:
self.removeCrystal()
return task.done
return task.cont
def move_to_player(self, task):
if self.lifetime < 0 or self.np.node() is None:
self.removeCrystal()
return task.done
direction = self.playerNode.getPos() - self.np.getPos()
idealVelocity = direction.normalized() * self.maxSpeed
accel = idealVelocity - self.np.node().getLinearVelocity()
self.np.node().applyCentralForce(accel)
return task.cont
def removeCrystal(self):
if self.np.node() is not None:
self.np.node().removeAllChildren()
base.world.remove(self.np.node())
self.removeAllTasks()
self.ignoreAll()