Skip to content

Instantly share code, notes, and snippets.

@zurgeg
Created May 24, 2020 18:29
Show Gist options
  • Save zurgeg/90f6cf05c084ad521db4622acc8c745c to your computer and use it in GitHub Desktop.
Save zurgeg/90f6cf05c084ad521db4622acc8c745c to your computer and use it in GitHub Desktop.
from math import pi, sin, cos
from direct.showbase.ShowBase import ShowBase
from direct.task import Task
from direct.actor.Actor import Actor
from direct.interval.IntervalGlobal import Sequence
from panda3d.core import Point3, Vec3
from panda3d.bullet import BulletWorld
from panda3d.bullet import BulletPlaneShape
from panda3d.bullet import BulletRigidBodyNode
from panda3d.bullet import BulletBoxShape
from panda3d.ode import OdeWorld
from panda3d.ode import OdeBody, OdeMass
import keyboard
from panda3d.bullet import BulletCharacterControllerNode
from panda3d.bullet import BulletCapsuleShape
from panda3d.bullet import ZUp
from panda3d.core import PointLight, AmbientLight
from panda3d.core import NodePath
from panda3d.core import PandaNode, LightNode, TextNode
from panda3d.core import LightRampAttrib, AuxBitplaneAttrib
from panda3d.core import DirectionalLight
from panda3d.core import Spotlight
from panda3d.core import PerspectiveLens
import random
from panda3d.core import BitMask32
from direct.particles.ParticleEffect import ParticleEffect
height = 1.75
radius = 0.4
class App(ShowBase):
def moveForward(self,catch):
print('w key pressed')
self.current_pos = (self.current_pos[0], self.current_pos[1] + 1,self.current_pos[2])
print(self.current_pos)
def moveBackward(self,catch):
print('w key pressed')
self.current_pos = (self.current_pos[0], self.current_pos[1] - 1,self.current_pos[2])
print(self.current_pos)
def moveLeft(self,catch):
print('w key pressed')
self.current_pos = (self.current_pos[0] + 1, self.current_pos[1],self.current_pos[2])
print(self.current_pos)
def moveRight(self,catch):
print('w key pressed')
self.current_pos = (self.current_pos[0] + 1, self.current_pos[1],self.current_pos[2])
print(self.current_pos)
def lookLeft(self,catch):
self.current_hpr = (self.current_hpr[0] - 1, self.current_hpr[1], self.current_hpr[2])
def lookRight(self,catch):
self.current_hpr = (self.current_hpr[0] + 1, self.current_hpr[1], self.current_hpr[2])
def lookUp(self,catch):
self.current_hpr = (self.current_hpr[0],self.current_hpr[1] - 1,self.current_hpr[2])
def moveUp(self,catch):
self.current_pos = (self.current_pos[0],self.current_pos[1],self.current_pos[2] + 1)
def lookUp(self,catch):
self.current_hpr = (self.current_hpr[0],self.current_hpr[1] + 1,self.current_hpr[2])
def lookDown(self,catch):
self.current_hpr = (self.current_hpr[0],self.current_hpr[1] - 1,self.current_hpr[2])
def fire(self,catch):
print("Fire!")
if self.pandaPos[0] == self.current_pos[0] and self.pandaPos[2] + 0.5 == self.current_pos[2] and not self.pandaPos[1] == self.current_pos[1] and not self.pandaPos[1] - 1 == self.current_pos[1] and not self.current_pos[1] - 1 == self.pandaPos[1] and not self.current_pos[1] > self.pandaPos[1]:
print('Oof')
def actovr(self,catch):
if not self.ovride:
self.ovride = True
else:
self.ovride = False
def gravPack(self,catch):
if not self.doGPack:
self.player.setGravity(1.0)
self.doGPack = True
else:
self.player.setGravity(0.0)
self.doGPack = False
def boostPack(self,catch):
if self.doBPack:
self.setLinearMovement(Vec3(0,0,1))
self.doBPack = False
else:
self.setLinearMovement(Vec3(0,0,0))
self.doBPack = True
def jump(self,catch):
self.player.setMaxJumpHeight(5.0)
self.player.setJumpSpeed(8.0)
self.player.doJump()
def __init__(self):
ShowBase.__init__(self)
self.doBPack = True
plightnode = PointLight("point light")
plightnode.setAttenuation((1, 0, 0))
self.enableParticles()
plight = render.attachNewNode(plightnode)
plight.setPos(30, -50, 0)
alightnode = AmbientLight("ambient light")
alightnode.setColor((0.8, 0.8, 0.8, 1))
alight = render.attachNewNode(alightnode)
render.setLight(alight)
render.setLight(plight)
render.setShaderAuto()
#self.word = BulletWorld()
self.cx = 0.0
self.cy = 0.0
self.render.setShaderAuto()
self.current_omega = 0
s = self.loader.loadSfx('Doom Soundtrack - Level 1 (Extended).mp3')
s.play()
self.ovride = False
self.current_pos = (10, 5, 1)
self.current_hpr = (45, -1, 0)
#self.fxboy = self.loader.loadModel("cubearm.egg")
self.world = BulletWorld()
self.world.setGravity(Vec3(0, 0, -9.81))
#self.physicsMgr.attachPhysicalNode(self.camera)
keyboard.on_press_key('w', self.moveForward)
keyboard.on_press_key('s', self.moveBackward)
keyboard.on_press_key('j',self.lookLeft)
keyboard.on_press_key('y',self.lookUp)
keyboard.on_press_key('h',self.lookDown)
keyboard.on_press_key('g',self.lookRight)
keyboard.on_press_key('d', self.moveLeft)
keyboard.on_press_key('e', self.actovr)
#self.fxboy.reparentTo(self.render)
#self.camera.reparentTo(self.render)
#self.camera.reparentTo(self.fxboy)
keyboard.on_press_key('a', self.moveRight)
keyboard.on_press_key('space', self.fire)
keyboard.on_press_key('2',self.jump)
#keyboard.on_press_key('y',self.lookUp)
keyboard.on_press_key('1',self.moveUp)
#keyboard.on_press_key('m',self.gravPack)
#keyboard.on_press_key('n',self.boostPack)
self.doGPack = False
self.basicobj = self.loader.loadModel("coolthing.egg")
self.basicobj.reparentTo(self.render)
self.basicobj.setScale(0.12,0.12,0.12)
self.mgb = BulletBoxShape(Vec3(0.12,0.12,0.12))
self.mgbb = BulletRigidBodyNode('MagnaBlock')
self.mgbb.setMass(12.0)
self.mgbb.addShape(self.mgb)
# self.basicobj.setPos(11.6041, -3.0922, 9.00042)
self.mgnp = render.attachNewNode(self.mgbb)
self.basicobj.reparentTo(self.mgnp)
self.mgbb.setGravity(Vec3(0,0,0))
self.cam = BulletCapsuleShape(radius, height - 2*radius, ZUp)
self.player = BulletCharacterControllerNode(self.cam,0.4,'Player')
self.playernp = self.render.attachNewNode(self.player)
self.world.attachCharacter(self.playernp.node())
self.camera.reparentTo(self.playernp)
self.playernp.setPos(self.current_pos)
self.playernp.setHpr(self.current_hpr)
self.playernp.setH(45)
#self.player.setGravity(0.0)
self.playernp.setCollideMask(BitMask32.allOn())
self.disableMouse()
'''
dlight = DirectionalLight('my dlight')
dlight.setColor((0.8, 0.8, 0.5, 1))
dlnp = render.attachNewNode(dlight)
self.render.setLight(dlnp)
self.plightnode = PointLight("point light")
self.plightnode.setAttenuation((0, 10, 0))
self.plight = render.attachNewNode(self.plightnode)
self.plight.setPos(-9, 41, -1)
self.alightnode = AmbientLight("ambient light")
self.alightnode.setColor((0.8, 0.8, 0.8, 1))
'''
self.scenes = BulletBoxShape(Vec3(0.25,0.25,0.25))
self.scenenode = BulletRigidBodyNode('Scene')
self.scenenode.setMass(12.0)
self.scenenode.addShape(self.scenes)
self.scenenp = render.attachNewNode(self.scenenode)
self.scenenp.setPos(-8,40,0)
self.world.attachRigidBody(self.scenenode)
self.scene = self.loader.loadModel("models/environment.egg.pz")
self.scene.reparentTo(self.render)
self.scene.setScale(0.25,0.25,0.25)
self.scene.setPos(-8,40,0)
self.basicobj.setPos((10,10,0.5))
self.scene.reparentTo(self.scenenp)
self.scenenode.setGravity(Vec3(0,0,0))
#self.taskMgr.add(self.spinCameraTask,"SpinCameraTask")
self.taskMgr.add(self.moveChar,"MoveChar")
self.taskMgr.add(self.moveCBod,"MoveCBod")
#self.pandaActor = Actor("cubearm.egg",{"walk":"cubearm4-ArmatureAction.egg"})
#self.pandaActor.setScale(0.12,0.12,0.12)
#self.pandaActor.setPos((10,10,0.5))
#self.pandaPos = (10,10,0.5)
#self.pandaActor.reparentTo(self.render)
#self.pandaActor.loop('walk')
'''
posInterval1 = self.pandaActor.posInterval(13,Point3(0,-10,0),startPos=Point3(0,10,0))
posInterval2 = self.pandaActor.posInterval(13,Point3(0,10,0),startPos=Point3(0,-10,0))
hprInterval1 = self.pandaActor.hprInterval(3,Point3(180,0,0),startHpr=Point3(0,0,0))
hprInterval2 = self.pandaActor.hprInterval(3,Point3(0,0,0),startHpr=Point3(180,0,0))
self.pandaPace = Sequence(posInterval1,hprInterval1,posInterval2,hprInterval2,name="pandaPace")
self.pandaPace.loop()
'''
self.p = ParticleEffect()
self.p.loadConfig('fireish.ptf')
self.p.start(parent = self.basicobj, renderParent=self.render)
def spinCameraTask(self,task):
print(task.time * 6.0)
angleDegrees = task.time * 6.0
angleRadians = angleDegrees * (pi / 180.0)
self.camera.setPos(20 * sin(angleRadians), -20 * cos(angleRadians),3)
self.camera.setHpr(angleDegrees,0,0)
return Task.cont
def moveChar(self,task):
if self.ovride:
speed = Vec3(0,0,0)
else:
speed = Vec3(0,0,0)
cts = Vec3(random.randint(1,5),random.randint(1,5),random.randint(0,1))
omega = self.current_omega
speed.setX(self.cx)
speed.setY(self.cy)
#self.camera.lookAt(self.basicobj.getPos())
#self.scenenp.setPos(-8,40,0)
print('[Scene]: ' + str(self.scenenp.getPos()))
print('[Cam]: ' + str(self.playernp.getPos()))
#self.player.setAngularMovement(omega)
#self.player.setLinearMovement(speed,True)
#self.mgbb.setLinearMovement(cts,True)
self.playernp.setPos(self.current_pos)
self.playernp.setHpr(self.current_hpr)
return task.cont
def moveCBod(self,task):
self.world.doPhysics(globalClock.getDt())
return task.cont
app = App()
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment