Package com.aqpproject.ia.game

Source Code of com.aqpproject.ia.game.IAController

/*
* AQP Project
* http://http://code.google.com/p/aqp-project/
* Alexandre Gomez - Clément Troesch - Fabrice Latterner
*/
package com.aqpproject.ia.game;

import com.aqpproject.game.Singleton;
import com.aqpproject.tools.Vector2D;
import com.aqpproject.visualisation.Visualisation;
import com.aqpproject.worldmodel.Control;

/**
*
* @author Clément
*/
public class IAController {

    public IAController(String playerName) {
        m_playerName = playerName;
        m_fireTime = -1;
        m_lastMovingTime = Singleton.getWorldModel().getTime();
        m_lastResetTime = 0;
        m_lastValidPosition = new Vector2D();
    }

    public void update() {
        if (Singleton.getWorldModel().isRaceStarted()) {
            if (m_lastResetTime == 0) {
                m_lastMovingTime = m_lastResetTime = Singleton.getWorldModel().getTime();
            }
            Vector2D pos = Singleton.getWorldModel().getEntityPosition(m_playerName);

            float rotation = Singleton.getWorldModel().getEntityRotation(m_playerName);
            rotation = rotation % 360;
            rotation = rotation >= 180 ? rotation - 360 : rotation;
            rotation = rotation <= 180 ? rotation : rotation + 360;
            int code = Singleton.getVisualisation().getTile(Visualisation.MAP_LAYER.IA, pos);

            move(rotation, getPerfectRotation(code));

            if (code >= 35 && code <= 42) {
                m_lastValidPosition = pos;
            }



        }
    }

    private void move(float currentRotation, float perfectRotation) {

        currentRotation = currentRotation % 360;
        perfectRotation = perfectRotation % 360;

        while (currentRotation < 0 || perfectRotation < 0) {
            currentRotation += 360;
            perfectRotation += 360;
        }

        float alpha = perfectRotation - currentRotation;

        if (Math.abs(alpha % 360) > 180) {
            perfectRotation -= (alpha / Math.abs(alpha)) * 360;
        }



        Vector2D vel = Singleton.getWorldModel().getEntityVelocity(m_playerName);

        if (currentRotation != perfectRotation) {
            if (currentRotation < perfectRotation) {
                Singleton.getWorldModel().control(Control.Q, m_playerName);
            } else {
                Singleton.getWorldModel().control(Control.D, m_playerName);
            }
        }

        float bias = Math.abs(alpha);
        bias = Math.abs((bias > 180) ? (bias - 360) : (bias));

        if (bias > ROTATION_CRITIC && vel.length() > 5) {
            Singleton.getWorldModel().control(Control.SHIFT_LEFT, m_playerName);
        } else {
            Singleton.getWorldModel().control(Control.Z, m_playerName);
        }

        //Singleton.getWorldModel().control(Control.Z, m_playerName);

        // System.out.println("C:"+currentRotation+"  /  P:"+perfectRotation);

        if (m_fireTime != -1 && m_fireTime < Singleton.getWorldModel().getTime()) {
            Singleton.getWorldModel().control(Control.SPACE, m_playerName);
        }

        if (m_fireTime == -1 && Singleton.getWorldModel().getEntityHasItem(m_playerName)) {
            m_fireTime = (float) (Singleton.getWorldModel().getTime() + (Math.random() * 10000.f) + 3000.f);
        }

        //CHECK RESET

//        System.out.println(vel.length());
        if (vel.length() > 5 || Singleton.getWorldModel().getTime() < m_lastResetTime + 3000) {
            m_lastMovingTime = Singleton.getWorldModel().getTime();
        }

        //RESET


        if (Singleton.getWorldModel().getTime() > m_lastMovingTime + 1000) {

            Vector2D pos = m_lastValidPosition.scale(1 / 64.f);
            m_lastValidPosition.x = Math.round(m_lastValidPosition.x);
            m_lastValidPosition.y = Math.round(m_lastValidPosition.y);
            m_lastValidPosition = m_lastValidPosition.scale(64);
            m_lastValidPosition = m_lastValidPosition.translate(32, 32);
            int code = Singleton.getVisualisation().getTile(Visualisation.MAP_LAYER.IA, pos);
            float goodRotation = getPerfectRotation(code);
            Singleton.getWorldModel().resetEntity(m_playerName, pos, goodRotation);
            m_lastResetTime = Singleton.getWorldModel().getTime();
        }





    }

    public float getPerfectRotation(int code) {
        float rotation = 0;
        if (code == Visualisation.IA_TILES.RIGHT.getCode()) {
            rotation = 0;
        } else if (code == Visualisation.IA_TILES.DOWN_RIGHT.getCode()) {
            rotation = -45;
        } else if (code == Visualisation.IA_TILES.DOWN.getCode()) {
            rotation = -90;
        } else if (code == Visualisation.IA_TILES.DOWN_LEFT.getCode()) {
            rotation = -135;
        } else if (code == Visualisation.IA_TILES.LEFT.getCode()) {
            rotation = -180;
        } else if (code == Visualisation.IA_TILES.TOP_LEFT.getCode()) {
            rotation = -225;
        } else if (code == Visualisation.IA_TILES.TOP.getCode()) {
            rotation = -270;

        } else if (code == Visualisation.IA_TILES.TOP_RIGHT.getCode()) {
            rotation = -315;
        }
        return rotation;

    }
    private String m_playerName;
    private final float ROTATION_CRITIC = 40;
    private float m_fireTime;
    private float m_lastMovingTime;
    private float m_lastResetTime;
    private Vector2D m_lastValidPosition;
}
TOP

Related Classes of com.aqpproject.ia.game.IAController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.