Help me solidify game-play mechanic before I construct demo.

Working code, re-distill into functions/classes?

I know this code if hard to read,
<em><strong>

I have not really taken any classes on python,

if I comment out my code completely can some one condense it into functions, and or classes that can then be ran as modules?

My code could be used as a starting point , or a total re-code that has the same end result is fine as well.

Part 1
Each component has 2 “layers”
a physics layer, (the actual component) and a Tmesh “Jack Map”

Basically I found that triangle mesh can not be compound parented,
and regular bound’s don’t return a material.

So in order to have mapped jacks, a item spawns a Tmesh
this item is encoded with ‘Component’ by the item which is it’s own GameObject.

So
add=scene.addObject(‘Tmesh’,own,0)
add[‘Component’]=own

then the Tmesh does

Tmesh.worldPosition=Component.worldPosition
Tmesh.worldOrientation=Comonent.worldOrientation

each frame. (the tmesh are on a seperate collision layer from physics)

so then, when attempting assembly, the player casts a ray, if it hits a component, it then casts a ray out of it’s Y axis.

if the ray hits a ‘Tmesh’ it looks up the 'Component" associate with the Tmesh, then it checks a property based on the Tmesh material it hit, to check if the jack is empty,

if it’s empty (Component[‘MAJack_x’]=False:)

Set true, then get a list of all of Component’s children - Component[‘Children’]

if it’s empty then

List1=Component_a+Component_b
Component_a[‘Parent’]=Component_b
get center of mass,

get mass of both items,

Parent kist to each items Component[‘Parent’]

Do Component_a[‘Children’]=Component_a.childrenRecurisve

(storying the hiarchy of assembly)

then I libNew A compound object center, and parent the list to it.

this makes a compound object with correct mass, center of mass and physics bound.

Step 2- Disassembly,

Create two lists when striking a component with “split” command

Component[‘Children’] and everything above Component in the hierarchy.

So then we build 2 Compound objects using the lists,

here is my code such as it is.


##Parent check
import bge
from mathutils import Vector
from bge import render
scene=bge.logic.getCurrentScene()
counter=scene.objects['Counter']
def get_centroid_and_mass(objects):
    centroid = Vector()
    mass = 0.0
    for obj in objects:
        if 'Mass' in obj and obj.name!="Parent":
            mass += obj['Mass']
            print(mass,obj)    
    for obj in objects:
         if 'Mass' in obj and obj.name!="Parent":
            influence = obj['Mass'] / mass
            centroid += obj.worldPosition * influence
    return centroid, mass


def join_objects(obj,target):
    
    
    children = obj['target_objects']
    
    if not hasattr(bge.logic, 'num_libnews'):
        bge.logic.num_libnews = 0
    parent_name = "Cube.005"
    
    targetx = obj
    parent = scene.addObject('Parent', targetx)
    new_mesh = bge.logic.LibNew(parent_name + str(bge.logic.num_libnews), 'Mesh', [parent_name])[0]
    parent.replaceMesh(new_mesh)
    bge.logic.num_libnews += 1
    
    
    counter['x']+=1
    centroid, mass = get_centroid_and_mass(children)
    print(centroid, mass)
    parent.worldPosition = centroid
    parent.mass=mass
    
    obj['Root']=parent
    obj['RootName']=parent.name
    target['Root']=parent
    target['RootName']=parent.name
    P2=target['Parent']
    
    target.setParent(parent,1,0)
    for objects in children:
        if objects.name!="Parent" and objects.name!=target:
            objects.removeParent()
            if objects['Parent']!="Empty":
                p=objects['Parent']
                objects.setParent(p,0,0)
                R=objects['Root']
            objects['Root']=parent
            objects['RootName']=parent.name
    for objects in children:
        
        objects['Children']=objects.childrenRecursive
        objects['ChildrenNames']=str(objects['Children'])
        objects.removeParent()
       
        objects.setParent(parent,1,0)
                
    if obj.name!="Parent":
        obj.setParent(parent, 1, 0)
    else:
        obj.applyMovement((0,0,-100),0) 
        ##obj.endObject()
    parent.worldLinearVelocity=(0,0,0)




def main():


    cont = bge.logic.getCurrentController()
    own = cont.owner
    scene=bge.logic.getCurrentScene()
    Mouse = cont.sensors['Mouse']
    Timer = cont.sensors['Property1']




    ##GetComponent A
    if Mouse.positive and Timer.positive:
        St = Vector ([0,1,0])
        Start = own.worldPosition+(own.worldOrientation * St)
        ##add= scene.addObject("HL",own,10)
        ##add.worldPosition=Start    
        St2 = Vector ([0,7.5,0])
        End = own.worldPosition+(own.worldOrientation * St2)
        ##add2= scene.addObject("HL",own,10)
        ##add2.worldPosition=End
        hitOb, hitPoint, hitNormal, hitPoly = own.rayCast(End, Start, 0, 'Tmesh', 0, 1, 1)
        render.drawLine(Start,End,(255,0,0))   
        if hitOb:
            H=hitOb
            print(H.name)
            if 'Tmesh' in H:
                if 'Component' in H:
                    if H.invalid==False:
                        T=H['Component']
                        if T.invalid==False:
                            target=H['Component']
                            print(target.name)
                            own['PtargetA']=target
                
                
                
        ##GetComponent B
        t = Vector ([0,1.5,0])
        Start2 = target.worldPosition+(target.worldOrientation * St)
        ##add= scene.addObject("HL",own,10)
        ##add.worldPosition=Start    
        St2 = Vector ([0,7.5,0])
        End2 = hitOb.worldPosition+(hitOb.worldOrientation * St2)
        hitOb2, hitPoint2, hitNormal2, hitPoly2 = hitOb.rayCast(End2, Start2, 0, 'Tmesh', 0, 1, 1)
        render.drawLine(Start2,End2,(0,0,255))
        if hitOb2:
            if 'Tmesh' in hitOb2:
                H2=hitOb2
                Component_b=H2['Component']
                own['PTargetB']=Component_b
        
        
        ## Get jack and check if occupied
        if hitPoly2 and hitOb2:
            hitMat = hitPoly2.material_name
            own['picup']=False
            own['HitMat']=hitMat
            if 'Jack_' in hitMat:
                print(hitMat)
                if 'hitMat' in Component_b:    
                    if Component_b[hitMat]==False:
                        target['hitMat']=hitMat
                        target['Parent']=Component_b
                        target['ParentName']=Component_b.name
                        print("Jack is empty")
                        Component_b[hitMat]=True
                        hitObPos=Component_b.worldPosition
                        spot=hitObPos+hitNormal2
                        target.alignAxisToVect(-hitNormal2, 1, 1)
                        list=[]
                        target.worldPosition=spot
                        hOffset = Vector([0,-1    ,0])
                        target.worldPosition += target.worldOrientation * hOffset
                        if Component_b['Root']!="Empty":
                            p=Component_b['Root']
                            if p.invalid==False:
                                Component_b=Component_b['Root']
                                list=Component_b.childrenRecursive
                       
                                
                        else:
                            list=[]    
                        list=list+[target]
                        list=list+[Component_b]
                        Component_b['target_objects']=list
                        join_objects(Component_b,target)
                    
main()



Attachments

CompoundAssemblyV101.blend (830 KB)

Unparent




##Split hierarchy
import bge
from mathutils import Vector
scene=bge.logic.getCurrentScene()

##get center of mass
def get_centroid_and_mass(objects):
centroid = Vector()
mass = 0.0
for obj in objects:
if ‘Mass’ in obj and obj.name!=“Parent”:
mass += obj[‘Mass’]
print(mass,obj)
for obj in objects:
if ‘Mass’ in obj and obj.name!=“Parent”:
influence = obj[‘Mass’] / mass
centroid += obj.worldPosition * influence
return centroid, mass
##Parent objects to “fake center”
def join_objects(obj):

children = obj['target_objects']
##Add fake root
if not hasattr(bge.logic, 'num_libnews'):
    bge.logic.num_libnews = 0
parent_name = "Cube.005"

targetx = obj
parent = scene.addObject('Parent', targetx)
new_mesh = bge.logic.LibNew(parent_name + str(bge.logic.num_libnews), 'Mesh', [parent_name])[0]
parent.replaceMesh(new_mesh)
bge.logic.num_libnews += 1
##get new center of mass and mass
centroid, mass = get_centroid_and_mass(children)

print(centroid, mass)
##Move Fake root to center of mass
parent.worldPosition = centroid

parent.mass = mass
## trying to ditch extra fake centers as ending them crashes blender
for objects in children:
    if objects.name!="Parent":
        objects.removeParent()
        if objects['Parent']!="Empty":
            p=objects['Parent']
            objects.setParent(p,0,0)
            
        objects['Root']=parent
        objects['RootName']=parent.name
for objects in children:
    
    objects['Children']=objects.childrenRecursive
    objects['ChildrenNames']=str(objects['Children'])
    objects.removeParent()
   
    objects.setParent(parent,1,0)

def main():

cont = bge.logic.getCurrentController()
own = cont.owner

Ray = cont.sensors[ 'Ray' ]
MouseR = cont.sensors[ 'Mouse1']


##selection of target of unparent logic
if Ray.positive and MouseR.positive and own[ 'Parenttimer' ] == 0:
    target=Ray.hitObject
    ## Tmesh object
    own['RParentTarget'] = target
    ## component assosiated with Tmesh flag
    Comp=target['Component']
    ## root assosiated with component
    print(Comp.name)
    list2=[]  
    list1=[]
    if 'Children' not in target:
        target['Children']=[]
    if 'Children' in Comp:
        list2=Comp['Children']
    
        
    store=Comp    
    if Comp['Root']!="Empty":
        
        Root=Comp['Root']
    else:
        Root=Comp
        
    if Root.children!=[]:          
        for items in Root.children:
            if items!=Comp:
                if items not in list2:
                    if items.name!="Parent":
                        list1=list1+[items]
        
    if Comp['Parent']!="Empty":
        P1=Comp['Parent']
    
        P1[Comp['hitMat']] = False
    
    
    
    
    print(list1)
    
    for items in Root.childrenRecursive:
        items.removeParent()
    
    if 'COM' in Root:
        Root.endObject()
          
    Comp.removeParent()
    Comp.reinstancePhysicsMesh()
    Comp.applyMovement((0,-1,.5),1)
    for items in list2:
        items.removeParent()
    list2=list2+[Comp]
    
    Comp['target_objects'] = list2
    Comp.removeParent()   
    print(list2)
    store['Parent']="Empty"
    if len(list2)&gt;1:
        store['Root']="Empty"
        join_objects(Comp)
    else:
        Comp['Root']="Empty"    
    if len(list1)&gt;1:
        
        Root['target_objects'] = list1 
        join_objects(Root)
        
    else:
        if 'COM' in Root:
            Root.endObject()
        if 'COM' not in Root:
            Root['Root']="Empty"    

main()

This project is growing up, and it is still intended to be a community driven development,

Help us make a great game?

The system created here will be available for use in any blender game, as it is mit 3.0 cc, name attributer. (so we can get credit’s for resumes)

Anyone game?