Hi all,
This is a basic path handler I wrote, since I didn’t feel like using the Motion Paths (which are loaded via .egg files). I wanted to have a system I could change within the game. I mainly wrote this as test for myself, and since I needed it. This is still very basic, and I’m going to update this atleast this evening again. Currently this includes:
- Adding/inserting paths to a Queue
- Start/stopping, pause/resume the path
- Going to a POS and ROTATE the camera
Here is the sourcecode:
from collections import deque
from direct.interval.IntervalGlobal import *
from pandac.PandaModules import *
'''
MovingPathHandler class
Usage: ([] = required, <> = optional)
MovingPathHandler.addPath("[POS] [(X,Y,Z)] <DURATION> <?>")
MovingPathHandler.addPath("[ROTATE] [DEGREES] <DURATION> <?>")
'''
class MovingPathHandler(deque):
def __init__(self, nodepath):
deque.__init__(self)
self.nodepath = nodepath
def addPath(self, path):
self.append(path)
def insertPath(self, path):
self.appendleft(path)
def stopAction(self):
self.sequence.finish()
def pauseAction(self):
self.sequence.pause()
def resumeAction(self):
self.sequence.resume()
def handlePath(self):
try:
action = self.popleft()
except IndexError:
#Queue is empty - resume.
return
if action.split()[0] == 'POS':
#HEADING to a position
position = action.split()[1]
x,y,z = tuple(map(int, position[1:-1].split(',')))
duration = 5
if action.split()[2] == 'DURATION':
duration = int(action.split()[3])
self.sequence = Sequence(self.nodepath.posInterval(duration,Point3(x,y,z),startPos=self.nodepath.getPos()),
Func(self.handlePath)).start()
if action.split()[0] == 'ROTATE':
#ROTATING TO A POS
degrees = int(action.split()[1])
duration = 1
if action.split()[2] == 'DURATION':
duration = int(action.split()[3])
self.sequence = Sequence(self.nodepath.hprInterval(duration,Point3(degrees, 0, 0)),
Func(self.handlePath)).start()
Here’s a tiny example:
from pandac.PandaModules import loadPrcFileData, KeyboardButton, MouseButton, WindowProperties, TextNode
from direct.gui.DirectGui import *
loadPrcFileData("", "window-title Qlade v0.01 - By wvd")
#loadPrcFileData("", """fullscreen 1 win-size 1024 768""")
import direct.directbase.DirectStart
from direct.task import Task
from direct.actor import Actor
import math
from MovingPathHandler import MovingPathHandler
from direct.particles.Particles import Particles
from direct.particles.ParticleEffect import ParticleEffect
#Load the first environment model
environ = loader.loadModel("models/environment")
environ.reparentTo(render)
environ.setScale(0.5,0.5,0.5)
environ.setPos(-8,42,0)
base.disableMouse()
base.camera.setPos(0,-100,20)
mp = MovingPathHandler(base.camera)
mp.addPath("POS (10,0,0) DURATION 5")
mp.addPath("ROTATE 45 DURATION 1")
mp.addPath("POS (10,10,0) DURATION 2")
mp.handlePath()
run()