Hello all!
I’ve written some kind of scrolling system for my rts game (just to understand what kind of scrolling with mouse) and i was asking myself if it is the most efficient way.
Really thank you!
snizzo
#added as a task later
def cameraMovements(self, task):
x = base.mouseWatcherNode.getMouseX()
y = base.mouseWatcherNode.getMouseY()
if x < -0.98:
camera.setX(camera.getX()-0.2)
if x > 0.98:
camera.setX(camera.getX()+0.2)
if y > 0.98:
camera.setZ(camera.getZ()+0.2)
if y < -0.98:
camera.setZ(camera.getZ()-0.2)
return task.cont
Here ya go, this is just a little class I whipped together to work in the game I’m making until I have more leisure to make a better, more efficient class for scrolling, rotating, and zooming in and out. Just be sure to call “update” on it every frame or so.
class mousecontrol(object):
aspect = 1.333
camnode = None #a node that the camera is parented to in another class
worldSize = None #the worldsize in an array like this: "[Xsize, Ysize]" (in panda units)
scrollSpeedMod = 35 #a modifier for the scrollspeed
def __init__( self, aspect = 1.333, camnode = None, worldSize = None ):
pass
self.aspect = aspect
self.camnode = camnode
self.worldSize = worldSize
self.mo = base.mouseWatcherNode
self.campos = self.camnode.getPos()
self.camhpr = base.camera.getHpr()
base.accept("mouse2", self.mousemiddleclickdown)
base.accept("mouse2-up", self.mousemiddleclickup)
base.accept("wheel_down", self.zoomout)
base.accept("wheel_up", self.zoomin)
pass
def update(self ):
self.updatemouse()
#the main functions
def updatemouse(self): #updates mouse stuff, called by update
if base.mouseWatcherNode.hasMouse():
self.dt = globalClock.getDt()
scrollspeed = (self.camnode.getZ() / self.scrollSpeedMod * self.dt * 25 + .02 )
selectedscrollspeed = 1
print scrollspeed
#print scrollspeed
self.campos = self.camnode.getPos()
self.camhpr = base.camera.getHpr()
if self.mo.getMouseX() <= -.97:
self.camnode.setX(self.camnode, -scrollspeed)
if self.getTerrainForCam(self.camnode.getPos()) == None:
self.camnode.setX(self.camnode, scrollspeed)
elif self.mo.getMouseX() >= .97:
self.camnode.setX(self.camnode, scrollspeed)
if self.getTerrainForCam(self.camnode.getPos()) == None:
self.camnode.setX(self.camnode, -scrollspeed)
if self.mo.getMouseY() <= -.97:
self.camnode.setY(self.camnode, -scrollspeed)
if self.getTerrainForCam(self.camnode.getPos()) == None:
self.camnode.setY(self.camnode, scrollspeed)
elif self.mo.getMouseY() >= .97:
self.camnode.setY(self.camnode, scrollspeed)
if self.getTerrainForCam(self.camnode.getPos()) == None:
self.camnode.setY(self.camnode, -scrollspeed)
pass
def zoomout(self):
if base.mouseWatcherNode.hasMouse():
if self.campos[2] < 150:
self.camnode.setZ((self.campos[2]/5)*6 + self.dt*8)
#print self.camnode.getPos()
def zoomin(self):
if base.mouseWatcherNode.hasMouse():
if self.campos[2] > 0:
self.camnode.setZ((self.campos[2]/6)*5 - self.dt*8)
def mousemiddleclickdown(self):
if base.mouseWatcherNode.hasMouse():
self.mousemiddleclickpointone = self.mo.getMouseX()
self.mousemiddleclickpoint = self.mo.getMouseX()
taskMgr.add(self.mousemiddleclickrotate, "rotatemiddle")
def mousemiddleclickrotate(self, task):
if base.mouseWatcherNode.hasMouse():
a = self.camnode.getH()
self.camnode.setH((self.mousemiddleclickpoint - self.mousemiddleclickpointone)/(self.dt*20) + a)
self.mousemiddleclickpoint = self.mo.getMouseX()
return task.again
def mousemiddleclickup(self):
if base.mouseWatcherNode.hasMouse():
if (base.taskMgr.hasTaskNamed("rotatemiddle")):
base.taskMgr.remove("rotatemiddle")
#utility functions
def getTerrainForCam(self, pos):
try:
if pos[0] >= -20 and pos[1] >= -20 and pos[0] <= self.worldSize[0]+20 and pos[1] <= self.worldSize[1]+20:
hey = 0
return hey
else:
return None
except:
return None
pass
from <module_name> import as <whatever_you_want_to_name_it>
import <module_name> as <whatever_you_want_to_name_it2>
import <module_name>
Then to call the class you have to use one of the following:
<whatever_you_want_to_name_it>(args)
<whatever_you_want_to_name_it2>.(args)
<module_name>.(args)
Or in your example assuming camera.py has a class named Camera:
from camera import Camera as photo
...stuff...
photo(args)
import camera as Camera
...stuff...
Camera.Camera(args)
import camera
...stuff...
camera.Camera(args)
Not to confuse the issue but can also instantiate the imported module’s class as a local or global as well within main.py such as self.camera = camera.Camera(args). So if you toggle between 1st person and 3rd person and you want those events to be handled in main.py you might call something like self.camera.SwitchToFirstPerson()