This does not put in error in the console.I put all the properties on the camera.Then put the controller in module mode for the script and it still does not work.What am i doing wrong?
from mathutils import Vector,Matrix
import bge
import random
THE_CUBE = bge.logic.getCurrentScene().objectsInactive["TheCube2"]
TYPE_TO_RGBA = {
"grass":(0.0, 1.0, 0.0, 1.0),
"wall": (0.2, 0.2, 0.3, 1.0),
"metal":(0.7, 0.7, 0.7, 1.0)
}
class VirtualCube2:
pass
class VC(VirtualCube2):
def __init__(self, position):
self.position = Vector(position)
self._inside = self._full = self._has_cube = False
self.obj = None
self.type = random.choice(["grass","wall","metal"])
@property
def inside(self):
return self._inside
@inside.setter
def inside(self, inside):
self._inside = bool(inside)
self.has_cube = self.inside and self.full
@property
def full(self):
return self._full
@full.setter
def full(self, full):
self._full = bool(full)
self.has_cube = self.inside and self.full
@inside.setter
def has_cube(self, has_cube):
has_cube = bool(has_cube)
if has_cube == self._has_cube:
return
self._has_cube = has_cube
if has_cube:
self.obj = bge.logic.getCurrentScene().addObject(THE_CUBE2, THE_CUBE2)
self.obj.worldPosition = self.position.copy()
self.obj.color = TYPE_TO_RGBA[self.type]
else:
obj = self.obj
self.obj = None
if obj and not obj.invalid:
obj.endObject()
if not self.full:
obj.scene.addObject("TheCubeDeath2",obj,10)
class CubeManager:
def __init__(self, cube_size=2):
self.coord = None
self.cube_size = int(cube_size)
self._distance = 0.0
self.offsets = []
self.inside_coords = set()
self.set_distance(10)
self.grid = {}
self.potential_cubes = 0
def chunky(self, pos):
base = self.cube_size
return tuple([round(v/base)*base for v in pos])
def set_distance(self, dist):
"""require some reinitialization(cost a bit)"""
if dist == self._distance:
return
dist = self._distance = float(dist)
offsets = []
amount = int(dist/self.cube_size)
for x in range(-amount,amount+1):
for y in range(-amount,amount+1):
for z in range(-amount,amount+1):
offset = Vector((x,y,z))*self.cube_size
offsets.append(offset)
self.offsets.clear()
self.offsets.extend(offsets)
if self.coord:
self.refresh()
self.potential_cubes = amount*amount*amount
def free_position(self, pos):
coord = self.chunky(pos)
if not coord in self.grid:
self.grid[coord] = VC(coord)
self.grid[coord].full = False
def fill_position(self, pos):
coord = self.chunky(pos)
if not coord in self.grid:
self.grid[coord] = VC(coord)
self.grid[coord].full = True
def extrude_position(self, pos, direction):
coord = self.chunky(pos)
if not coord in self.grid:
return
if not self.grid[coord].full:
return
dr = Vector(direction).normalized()
other_coord = self.chunky(Vector(coord)+dr*self.cube_size)
if other_coord == self.coord:
return
if not other_coord in self.grid:
self.grid[other_coord] = VC(other_coord)
self.grid[other_coord].type = self.grid[coord].type
self.grid[other_coord].full = True
self.grid[other_coord].inside = True
self.inside_coords.add(other_coord)
def update_position(self, pos):
assert len(pos) == 3
if self.coord is None:
self.free_position(pos) # where should be the player ..
coord = self.chunky(pos)
if coord != self.coord:
self.coord = coord
self.refresh()
def refresh(self):
pos = Vector(self.coord)
inside_coords = {self.chunky(pos+offset) for offset in self.offsets}
for coord in inside_coords:
if not coord in self.grid:
self.grid[coord] = VC(coord)
self.grid[coord].full = True
self.grid[coord].inside = True
for coord in self.inside_coords - inside_coords:
self.grid[coord].inside = False
self.inside_coords = inside_coords
def mo(cont):
own = cont.owner
if not "cm" in own:
own["cm"] = CubeManager(4)
own["cm"].set_distance(2.0) # warning to the correlation cube_size and distance,distance big = lag
#own["cm"].free_position(own.worldPosition)
cm = own["cm"]
cm.update_position(own.worldPosition)
### just for debug / stress test (how many cubes can be handled?)
if bge.logic.mouse.events[bge.events.WHEELUPMOUSE] == 2:
cm.set_distance(cm._distance*0.9)
if bge.logic.mouse.events[bge.events.WHEELDOWNMOUSE] == 2:
cm.set_distance(cm._distance*1.1)
own["POTENTIAL_CUBES2"] = cm.potential_cubes # this is what can waste the rasterizer and also bullet
own["tot_virtual_cubes2"] = len(cm.grid) # this can eventually waste the ram (can become a problem after a week of digging)
###############
####### digging
ray_start = own.worldPosition
ray_end = ray_start -own.worldOrientation.col[2] * 30.0
ho,hp,hn = own.rayCast(ray_end,ray_start)
if ho:
if ho.name == "TheCube2":
#ok we have intercepted the right cube
# remove
if bge.logic.mouse.events[bge.events.LEFTMOUSE] == 1:#click
# let to the cubemanager of remove cube using the position of cube , not the cube direcly
cm.free_position(ho.worldPosition)
# is veeery assumed that the cubes not moves for any reason
#duplicate
elif bge.logic.mouse.events[bge.events.RIGHTMOUSE] == 1:
cm.extrude_position(ho.worldPosition,hn) # position, normal
def mave(cont):
own = cont.owner
kev = bge.logic.keyboard.events
accel = 1.5
w, s, a, d = [kev[ord(k)]==2 for k in "wsad"]
vel = own.localLinearVelocity
vel.xy *= 0.9
vel.y += (w-s) * accel
vel.x += (d-a) * accel