Final Project, Educational

Hi! i’m a student of Multimedia Engineering, i need some help with a program.

I have a Control model view organization about my project, and everything that i do in the main class i need to do in separated classes, now i have literally almost everything, but i need one thing, when I call the method setUpKeys in my Main class works well, but i my character doesn’t walk, and that’s because my onAction method, and i don’t know what can i do to call or to use my onAction Method

    public class Pacman implements ActionListener {

    public Node pacman;
    public CharacterControl character;
    public CapsuleCollisionShape capsule;
    boolean up = false;
    boolean down = false;
    boolean right = false;
    boolean left = false;

    public Pacman(Node pacman, CharacterControl character, CapsuleCollisionShape capsule) {
        this.pacman = pacman;
        this.character = character;
        this.capsule = capsule;
    }

    public Pacman() {
    }

    public Node getPacman() {
        return pacman;
    }

    public void setPacman(Node pacman) {
        this.pacman = pacman;
    }

    public CharacterControl getCharacter() {
        return character;
    }

    public void setCharacter(CharacterControl character) {
        this.character = character;
    }

    public CapsuleCollisionShape getCapsule() {
        return capsule;
    }

    public void setCapsule(CapsuleCollisionShape capsule) {
        this.capsule = capsule;
    }

    public boolean isUp() {
        return up;
    }

    public void setUp(boolean up) {
        this.up = up;
    }

    public boolean isDown() {
        return down;
    }

    public void setDown(boolean down) {
        this.down = down;
    }

    public boolean isRight() {
        return right;
    }

    public void setRight(boolean right) {
        this.right = right;
    }

    public boolean isLeft() {
        return left;
    }

    public void setLeft(boolean left) {
        this.left = left;
    }

    public void crearPacman(AssetManager assetManager, Node rootNode, BulletAppState bulletAppState) {
        
        capsule = new CapsuleCollisionShape(3f, 4f);
        pacman = (Node) assetManager.loadModel("Models/Pac-Man/PacmanHigh.j3o");
        character = new CharacterControl(capsule, 0.01f);
        pacman.setLocalScale(6f);
        pacman.addControl(character);
        pacman.setLocalTranslation(3f, 5, 0);
        character.setPhysicsLocation(new Vector3f(3f, 5, 0));
        rootNode.attachChild(pacman);
        bulletAppState.getPhysicsSpace().add(character);
        
        

    }

    public void setUpKeys(InputManager inputManager) {
        inputManager.addMapping("CharLeft", new KeyTrigger(KeyInput.KEY_A));
        inputManager.addMapping("CharRight", new KeyTrigger(KeyInput.KEY_D));
        inputManager.addMapping("CharUp", new KeyTrigger(KeyInput.KEY_W));
        inputManager.addMapping("CharDown", new KeyTrigger(KeyInput.KEY_S));
        inputManager.addListener(this, "CharLeft");
        inputManager.addListener(this, "CharRight");
        inputManager.addListener(this, "CharUp");
        inputManager.addListener(this, "CharDown");


    }

    public void onAction(String name, boolean value, float tpf) {
        if (name.equals("CharLeft")) {
            if (value) {
                left = true;
            } else {
                left = false;
            }
        } else if (name.equals("CharRight")) {
            if (value) {
                right = true;
            } else {
                right = false;
            }
        } else if (name.equals("CharUp")) {
            if (value) {
                up = true;
            } else {
                up = false;
            }
        } else if (name.equals("CharDown")) {
            if (value) {
                down = true;
            } else {
                down = false;
            }
        }
    }
}

//main class

    public class Main extends SimpleApplication  {

    BulletAppState bulletAppState;
    String name;
    boolean value;
    float tpf;
    
    Pacman pc;
    Escenario es;
    
    Vector3f walkDirection = new Vector3f();
    ChaseCamera chaseCam;

    public static void main(String[] args) {
        Main app = new Main();
        app.start();
    }
//metodo principal de inicio de la aplicacion
    @Override
    public void simpleInitApp() {
        pc = new Pacman();
        es = new Escenario();
        


        bulletAppState = new BulletAppState();
        bulletAppState.setThreadingType(BulletAppState.ThreadingType.PARALLEL);
        stateManager.attach(bulletAppState);
        
        pc.setUpKeys(inputManager);
       
        es.crearEscenario(assetManager, rootNode, bulletAppState);
        es.crearLuz(rootNode, assetManager, viewPort);      
        pc.crearPacman(assetManager, rootNode, bulletAppState); 
        setupChaseCamera();
                
    }
//metodo para el seguimiento de la cámara
    private void setupChaseCamera() {

        chaseCam = new ChaseCamera(cam, pc.getPacman(), inputManager);
        chaseCam.setDefaultDistance(15);
        chaseCam.setDefaultHorizontalRotation((float) Math.toRadians(270));

        chaseCam.setLookAtOffset(Vector3f.ZERO);
        flyCam.setEnabled(false);

    }

    @Override
    public void simpleUpdate(float tpf) {
        Vector3f camDir = cam.getDirection().clone();
        Vector3f camLeft = cam.getLeft().clone();
        camDir.y = 0;
        camLeft.y = 0;
        walkDirection.set(0, 0, 0);
        if (pc.isLeft()) {
            walkDirection.addLocal(camLeft);
        }
        if (pc.isRight()) {
            walkDirection.addLocal(camLeft.negate());
        }
        if (pc.isUp()) {
            walkDirection.addLocal(camDir);
        }
        if (pc.isDown()) {
            walkDirection.addLocal(camDir.negate());
        }

        pc.character.setViewDirection(walkDirection);

    }

    @Override
    public void simpleRender(RenderManager rm) {
        //TODO: add render code
    }

    
}

I’m not too familiar with CharacterControl (it’s recommended to use BetterCharacterControl) but it seems a bit odd to call setViewDirection with walkDirection. Anyways, I’d set up some debugging to see where things fail.

For your listener to be called you need to register it. Please go through the input related tutorials (and several others). They explain all of this in some detail.

1 Like