More Spatial objects with the same AI

My question is: how do i make it so that i can use the same makeCharacter method to make for example 5 golems and use my golemAi method for all those golems?

protected Spatial makeCharacter(float x, float y, float z) {
    	    golem = assetManager.loadModel("Models/Oto/Oto.mesh.xml");
    	    golem.scale(0.5f);
    	    golem.setName("piet");
    	    golem.setLocalTranslation(x, y, z);
    	    // We must add a light to make the model visible
    	    DirectionalLight sun = new DirectionalLight();
    	    sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f));
    	    golem.addLight(sun);
    	    
    	    control = golem.getControl(AnimControl.class);
    	    channel = control.createChannel();
    	    channel.setAnim("Walk");
    	    rootNode.attachChild(golem);
    	    return golem;
    	  }
     public void golemAi(float tpf){
    	//golem AI
          playerLocation = cam.getLocation();
          playerLocationX = playerLocation.x;
          playerLocationZ = playerLocation.z;
          
          golemLocation = golem.getLocalTranslation();
          golemLocationX = golemLocation.x;
          golemLocationZ = golemLocation.z;
          
          deltaGolemLocationX = playerLocationX - golemLocationX;
          deltaGolemLocationZ = playerLocationZ - golemLocationZ;
          
          if(playerLocationX != golemLocationX && deltaGolemLocationX > 0){
          	golem.move(tpf * 2, 0, 0);
          	golem.lookAt(cam.getLocation(), new Vector3f(0,0,0));
          }
         
          if(playerLocationZ != golemLocationZ && deltaGolemLocationZ > 0){
          	golem.move(0, 0, tpf * 2);
          }
          
          if(playerLocationX != golemLocationX && deltaGolemLocationX < 0){
          	golem.move(-tpf * 2, 0, 0);
          	golem.lookAt(cam.getLocation(), new Vector3f(0,0,0));
          }
          
          if(playerLocationZ != golemLocationZ && deltaGolemLocationZ < 0){
          	golem.move(0, 0, -tpf * 2);
          }
          //print score on screen 
    	    DeathText = new BitmapText(guiFont, false);
    	    DeathText.setSize(guiFont.getCharSet().getRenderedSize());
    	    DeathText.setColor(ColorRGBA.Red);
    	    DeathText.setLocalTranslation(300, hudText.getLineHeight(), 0);
    		if((deltaGolemLocationX <= 0.2f || deltaGolemLocationX <= -0.2f) && (deltaGolemLocationZ <= 0.2f || deltaGolemLocationZ <= -0.2f)){
    			rootNode.detachAllChildren();
    			guiNode.detachAllChildren();
    			DeathText.setText("YOU JUST GOT KNOCKED THE FUCK OUT BOY! You killed:" + golemKills + "BITCHES");
    			guiNode.attachChild(DeathText);
    		}
      }

You may want to use Controls. Just create a GolemAI control and add a new instance of it to every golem spatial.

1 Like