Cloning an extended object

Started by fireside, April 30, 2009, 07:28:41 PM

Previous topic - Next topic

fireside

I have this monster in my game that is extended a ways from Object3d.  The class right above it is called Actor and the next extension is called OneEye.  So, I'm wondering how to duplicate it now.  My first effort, which probably seems silly is to do:
        oneEye1 = new OneEye(loadModel("mon1i1.3ds", 3));
        oneEye1.translate(0, -10, 0);
        oneEye1.translate(m2w(10, 10));
        oneEye1.lastX = 10;
        oneEye1.lastZ = 10;
        oneEye1.setMap(10, 10);
        oneEye1.rotateY(3.14f);
        world.addObject(oneEye1);
        enemies.add(oneEye1);
        oneEye1 = new OneEye(loadModel("mon1i1.3ds", 3));
        oneEye1.translate(0, -10, 0);
        oneEye1.translate(m2w(20, 10));
        oneEye1.lastX = 20;
        oneEye1.lastZ = 10;
        oneEye1.setMap(20, 10);
        oneEye1.rotateY(3.14f);
        world.addObject(oneEye1);
        enemies.add(oneEye1);


I know I could use a for loop, but I think I'm loading everything twice that way.  All the animation meshes, etc.  Is there some way around that?  I load the animations in the class when it starts.
click here->Fireside 7 Games<-

EgonOlsen

If i have to do somthing like this, i.e. create multiple instances of a class that extends Object3D based on a loaded mesh, i add a static Object3D to the class as blue print, load that in a static{....} section on top of the class and use it in the actual constructor. Looks something like this:


private static Object3D bluePrint;
....
static {
   bluePrint=Loader.load3DS(....)[0]; // Or whatever...
   bluePrint.set...;
   bluePrint.set...;
}

public MyObject() {
   super(bluePrint);
   ...
}



The source code for Robombs contains some examples for this. I'm not sure if this is the best or even an elegant solution, but it worked for me (tm).

fireside

click here->Fireside 7 Games<-

fireside

#3
When I try to do anything in the constructor, it says that the call to super has to be the first statement.  I am passing it an object3d, but I think I only did that because it said there was no constructor in the next class down that had nothing for constructor. 
click here->Fireside 7 Games<-

EgonOlsen

Quote from: fireside on April 30, 2009, 10:16:56 PM
When I try to do anything in the constructor, it says that the call to super has to be the first statement.
Of course. But that usually isn't a problem. Here's an example from Robombs:


    private static Object3D bluePrint = null;
    private static Object3D bluePrintFuse = null;

    ...

    static {
        // Static initializer for bomb blueprint
        bluePrint = Primitives.getSphere(12, RADIUS);
        bluePrint.getMesh().compress();
        bluePrint.rotateX((float) Math.PI);
        bluePrint.rotateMesh();
        bluePrint.setRotationMatrix(new Matrix());
        bluePrint.calcTextureWrapSpherical();
        bluePrint.setTexture("bomb1");
        bluePrint.getMesh().compress();
        bluePrint.build();
       
        bluePrintFuse=Primitives.getCylinder(6, 0.2f, 7);
        bluePrintFuse.rotateZ(0.5f);
        bluePrintFuse.translate(-2,-4.5f,0 );
        bluePrintFuse.translateMesh();
        bluePrintFuse.setTranslationMatrix(new Matrix());
        bluePrintFuse.getMesh().compress();
        bluePrintFuse.setTexture("bomb1");
        bluePrintFuse.calcTextureWrapSpherical();
        bluePrintFuse.build();
       
        bluePrint.translate(100000, 100000, 100000);
    }

    /**
     * Creates a new bomb based on the blueprint.
     */
    public BombView() {
        super(bluePrint, bluePrintFuse, true);
        createCollisionMesh();
    }
   
    public BombView(ClientObject obj) {
        super(obj, bluePrintFuse, true);
        createCollisionMesh();
    }

    ....

fireside

OK, I didn't read it close enough.  I'll try again.
click here->Fireside 7 Games<-