Package com.aqpproject.worldmodel.game.entity

Source Code of com.aqpproject.worldmodel.game.entity.WECar

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

import com.aqpproject.game.Singleton;
import com.aqpproject.tools.Color;
import com.aqpproject.tools.Interpolation;
import com.aqpproject.tools.Vector2D;
import com.aqpproject.visualisation.Visualisation;
import com.aqpproject.worldmodel.data.Car;
import com.aqpproject.worldmodel.data.CheckPointManager;
import com.aqpproject.worldmodel.data.Checkpoint;
import com.aqpproject.worldmodel.data.Inventory;
import com.aqpproject.worldmodel.game.state.RaceGameState;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;

/**
* Player class
*
* @author Alexandre, Clement, Fabrice
*/
public class WECar extends WorldPhysicEntity implements Comparable {

    /**
     * @see WorldEntity#WorldEntity(java.lang.String, java.lang.String,
     * com.aqpproject.tools.Vector2D, com.aqpproject.tools.Vector2D, float,
     * float)
     */
    public WECar(String name, Vector2D position, float rotation, Car car, int frame, RaceGameState state, String playerName) {
        super(name, car.m_spriteName, position, rotation, car.m_size, car.m_density, car.m_linearDamping, car.m_angularDamping, frame);
        m_linearSpeed = car.m_linearSpeed;
        m_angularSpeed = car.m_angularSpeed;
        m_moveBackward = false;
        m_checkpointManager = new CheckPointManager();
        m_state = state;
        m_shift = false;
        m_textActorName = name + "_info";
        m_playerName = playerName;
        m_powerDown = m_boost = false;
        m_lastFire = 0;
        m_lastStaticCollision = 0;
        m_sparklePosition = new Vector2D();
        m_lastContactPoint = new Vector2D();
        m_isStaticContact = false;
        m_isMyCar = false;
        m_lastTime = Singleton.getWorldModel().getTime();
        m_random = 0;
        m_sound = "defaut";
        Singleton.getVisualisation().setActorShadowSize(name, 4);

        Singleton.getVisualisation().createTextActorPerso(m_textActorName, m_playerName, 64, m_position.x - 32, m_position.y + 64, "ar15whi", false);

//        Singleton.getVisualisation().createLineActor(m_lineActorName, 500,500,1000,1000, false);

        try {
            m_inventory = new Inventory(1, "config/items.xml", m_state, this);
        } catch (ParserConfigurationException | SAXException | IOException ex) {
            Logger.getLogger(RaceGameState.class.getName()).log(Level.SEVERE, null, ex);
        }

        //Sparkles
        Singleton.getVisualisation().createParticleActor(m_name + "_sparkles", "sparkles", m_position.x, m_position.y, false);

        //Durt
        Singleton.getVisualisation().createParticleActor(m_name + "_durt", "durt", m_position.x, m_position.y, false);

        //Smoke
        Singleton.getVisualisation().createParticleActor(m_name + "_burn", "smoke", m_position.x, m_position.y, false);

    }

    @Override
    public void destroy() {
        super.destroy();
        Singleton.getVisualisation().deleteTextActor(m_textActorName, false);
        //Singleton.getVisualisation().deleteLineActor(m_lineActorName, false);
        if (m_powerDown || m_boost) {
            Singleton.getVisualisation().deleteParticleActor(m_name + "_smoke", false);
        }
        Singleton.getVisualisation().deleteParticleActor(m_name + "_sparkles", false);
        Singleton.getVisualisation().deleteParticleActor(m_name + "_durt", false);
        Singleton.getVisualisation().deleteParticleActor(m_name + "_burn", false);
    }

    /**
     * @see WorldEntity#update(long)
     */
    @Override
    public void update(long time) {
        super.update(time);
        int road = Singleton.getVisualisation().getTile(Visualisation.MAP_LAYER.ROAD, new Vector2D(m_position).translate(32, 0));
        //If out of the road
        if (road == 0) {
            Singleton.getPhysics().setDampling(m_name, m_linearDamping + 1.f, m_angularDamping);
            //DURT
            m_durt = Singleton.getPhysics().getLinearVelocity(m_name).length() > 50;
        } else {
            Singleton.getPhysics().setDampling(m_name, m_linearDamping, m_angularDamping);
            m_durt = false;
        }

        //Valeur de la variable random
        m_random = (int) (Singleton.getWorldModel().getTime() % 4);

        //Checkpoints
        int check = Singleton.getVisualisation().getTile(Visualisation.MAP_LAYER.INFORMATIONS, getCenter());
        if (check >= 15 && check <= 24) {
            check -= 15;
            m_checkpointManager.addCheckPoint(this, check);




        } // ITEMS
        else if (check == 14 && (Singleton.getWorldModel().getTime() - m_lastFire) > 200) {
            Vector2D tilePosition = new Vector2D();

            tilePosition.x = (float) ((Math.floor(getCenter().x / 64.f)) * 64);
            tilePosition.y = (float) ((Math.floor(getCenter().y / 64.f)) * 64);
            String name = "box_" + tilePosition.x + "_" + tilePosition.y;

            if (m_state.getWorldEntities().containsKey(name)) {
                m_inventory.add((WEBox) m_state.getWorldEntities().get(name));

            }

        }

        //PowerDown
        if (Singleton.getOptionsController().getRole().equals("SERVER")) {
            if (m_powerDown) {
                if ((time - m_powerDownStart) > 5000) {
                    m_powerDown = false;
                    Singleton.getVisualisation().deleteParticleActor(m_name + "_smoke", true);
                }
            } else if (m_boost) {
                if ((time - m_boostStart) > 5000) {
                    m_boost = false;
                    Singleton.getVisualisation().deleteParticleActor(m_name + "_smoke", true);
                }
            }
        }

        //TEXT

        //STATIC COLLISION
        if (Singleton.getOptionsController().getRole().equals("SERVER")) {
            m_isColliding = (Singleton.getWorldModel().getTime() - m_lastStaticCollision) < 200;
        }



    }

    @Override
    public void updateActorTransform() {
        super.updateActorTransform();
        Vector2D pos = Interpolation.getPositionInterpolation(m_oldPosition, m_position, Singleton.getWorldModel().getInterpolation());
        Singleton.getVisualisation().updateTextActor(m_textActorName, m_playerName, getCenter().x - 32, getCenter().y + 64);//pos.x+32, pos.y+64);

        Vector2D center = getCenter();

        if (m_powerDown) {
            Singleton.getVisualisation().setActorTeint(m_name, new Color(0, 1, 0, 1));
            Singleton.getVisualisation().updateParticleActor(m_name + "_smoke", center.x, center.y, true);
        } else if (m_boost) {
            center.translate(getDirection().scale(-32));
            Singleton.getVisualisation().updateParticleActor(m_name + "_smoke", center.x, center.y, true);
        } else {
            Singleton.getVisualisation().setActorTeint(m_name, new Color(1, 1, 1, 1));
        }

        center = getCenter();
        center.translate(getDirection().scale(-32));
        Singleton.getVisualisation().updateParticleActor(m_name + "_durt", center.x, center.y, m_durt);

        Singleton.getVisualisation().updateParticleActor(m_name + "_burn", center.x, center.y, m_shift && Singleton.getPhysics().getLinearVelocity(m_name).length() > 50);

        center = getCenter();
        Singleton.getVisualisation().updateParticleActor(m_name + "_sparkles", m_sparklePosition.x + center.x, m_sparklePosition.y + center.y, m_isColliding);
    }

    /**
     * Move the entity forward
     */
    @Override
    public void moveForward() {
        Singleton.getPhysics().applyLinearForce(m_name, getDirection().scale(getLinearSpeed()));
        m_moveBackward = false;
    }

    /**
     * Move the entity forward with boost
     */
    @Override
    public void useItem() {
//        Singleton.getPhysics().applyLinearForce(m_name, getDirection().scale(m_linearSpeed / 2));
//        m_moveBackward = false;
        m_inventory.use();
        m_lastFire = Singleton.getWorldModel().getTime();
    }

    @Override
    public void shift() {
        m_shift = true;
    }

    /**
     * Move the entity backward
     */
    @Override
    public void moveBackward() {
        Singleton.getPhysics().applyLinearForce(m_name, getDirection().scale(-getLinearSpeed() / 2));
        m_moveBackward = true;
    }

    /**
     * Rotate the entity left
     */
    @Override
    public void rotateLeft() {
        Singleton.getPhysics().applyAngularForce(m_name, getRotationSpeed());
        m_shift = false;
    }

    /**
     * Rotate the entity right
     */
    @Override
    public void rotateRight() {
        Singleton.getPhysics().applyAngularForce(m_name, -getRotationSpeed());
        m_shift = false;
    }

    private float getLinearSpeed() {
        float speed = m_linearSpeed;
        if (m_powerDown) {
            speed /= 2;
        } else if (m_boost) {
            speed *= 1.5;
        }
        if (m_isColliding) {
            speed /= 2;

            if (m_isMyCar) {

                Singleton.getInput().setVibration(30000, 30000);

                if (m_lastTime + 1000 < Singleton.getWorldModel().getTime()) {

                    switch (m_random) {
                        case 0:
                            m_sound = "crash01";
                            break;
                        case 1:
                            m_sound = "crash02";
                            break;
                        case 2:
                            m_sound = "crash03";
                            break;
                        case 3:
                            m_sound = "crash04";
                            break;
                    }

                    try {
                        Singleton.getAudioController().playSound(m_sound, false);
                        m_lastTime = Singleton.getWorldModel().getTime();
                    } catch (ParserConfigurationException | SAXException | IOException ex) {
                        Logger.getLogger(WECar.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    m_lastTime = Singleton.getWorldModel().getTime();
                }
            }
        }

        return speed;
    }

    private float getRotationSpeed() {
        float speed = Math.min(12, Singleton.getPhysics().getLinearVelocity(m_name).scale(1.f / 100.f).length() * m_angularSpeed);
        if (m_shift) {
            speed *= 2.f;
        }

        if (m_boost) {
            speed *= 1.5;
        }
        return speed * (m_moveBackward ? -1 : 1);
    }

    public boolean isInTheWrongDirection() {
        return false;
    }

    public int getLap() {
        return m_checkpointManager.getLap();
    }

    @Override
    public void startContact(WorldPhysicEntity other, Vector2D normal, Vector2D point) {
        if (other instanceof WECar) {
            m_lastContactPoint = point;
            m_isStaticContact = false;
            Vector2D center = getCenter();
            m_sparklePosition = new Vector2D(point).translate(-center.x, -center.y);
            m_lastStaticCollision = Singleton.getWorldModel().getTime();
            if (m_isMyCar) {

                Singleton.getInput().setVibration(30000, 30000);
            }
        }
    }

    @Override
    public void stopContact(WorldPhysicEntity other, Vector2D normal, Vector2D point) {
    }

    @Override
    public void startStaticContact(Vector2D normal, Vector2D point) {
        m_lastContactPoint = point;
        m_isStaticContact = true;

        Vector2D center = getCenter();
        m_sparklePosition = point.translate(-center.x, -center.y);
        m_lastStaticCollision = Singleton.getWorldModel().getTime();
    }

    @Override
    public void stopStaticContact(Vector2D normal, Vector2D point) {
    }

    public void applyPowerDown() {
        m_powerDownStart = Singleton.getWorldModel().getTime();
        if (m_boost || m_powerDown) {
            Singleton.getVisualisation().deleteParticleActor(m_name + "_smoke", true);
        }
        m_powerDown = true;
        m_boost = false;
        Singleton.getVisualisation().createParticleActor(m_name + "_smoke", "powerDown", m_position.x, m_position.y, true);

        try {
            Singleton.getAudioController().playSound("mouahah", false);
        } catch (ParserConfigurationException | SAXException | IOException ex) {
            Logger.getLogger(WECounter.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    public void applyBoost() {

        m_boostStart = Singleton.getWorldModel().getTime();
        if (m_powerDown || m_boost) {
            Singleton.getVisualisation().deleteParticleActor(m_name + "_smoke", true);
        }
        Singleton.getVisualisation().createParticleActor(m_name + "_smoke", "boost", m_position.x, m_position.y, true);
        m_powerDown = false;
        m_boost = true;
        if (isMyCar()) {
            try {
                Singleton.getAudioController().playSound("boost", false);
            } catch (ParserConfigurationException | SAXException | IOException ex) {
                Logger.getLogger(WECar.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public Checkpoint getLastCheckpoint() {
        return m_checkpointManager.getLastCheckPoint();
    }

    @Override
    public int compareTo(Object o) {
        WECar c = (WECar) o;
        if (m_checkpointManager.hasCheckpoint() && c.m_checkpointManager.hasCheckpoint()) {
            return this.getLastCheckpoint().compareTo(c.getLastCheckpoint());
        } else if (!m_checkpointManager.hasCheckpoint() && !c.m_checkpointManager.hasCheckpoint()) {
            return 0;
        } else if (!m_checkpointManager.hasCheckpoint()) {
            return 1;
        } else {
            return -1;
        }

    }

    public Inventory getInventory() {
        return m_inventory;
    }

    public void setColliding(boolean b) {
        m_isColliding = b;
    }

    public boolean getColliding() {
        return m_isColliding;
    }

    public void setSparklingPosition(Vector2D v, boolean isStatic) {
        Vector2D center = getCenter();
        if (isStatic) {
            m_sparklePosition = v;
        } else {
            m_sparklePosition = v.translate(-center.x, -center.y);
        }
    }

    public Vector2D getContactPoint() {
        return m_lastContactPoint;
    }

    public boolean getIsStaticContact() {
        return m_isStaticContact;
    }

    public boolean getPowerDown() {
        return m_powerDown;
    }

    public boolean getBoost() {
        return m_boost;
    }

    public void deletePowerDown() {
        Singleton.getVisualisation().deleteParticleActor(m_name + "_smoke", true);
        m_powerDown = false;
    }

    public void deleteBoost() {
        Singleton.getVisualisation().deleteParticleActor(m_name + "_smoke", true);
        m_boost = false;
    }

    public String getPlayerName() {
        return m_playerName;
    }

    public void setMyCar(boolean myCar) {
        m_isMyCar = myCar;
    }

    public boolean isMyCar() {
        return m_isMyCar;
    }
    /////////////////////////////////////////
    // Attributes
    /////////////////////////////////////////
    private boolean m_moveBackward;
    private float m_linearSpeed;
    private float m_angularSpeed;
    private CheckPointManager m_checkpointManager;
    protected Inventory m_inventory;
    private RaceGameState m_state;
    private boolean m_shift;
    private String m_playerName;
    private String m_textActorName;
    private long m_powerDownStart;
    private boolean m_powerDown;
    private long m_boostStart;
    private boolean m_boost;
    private long m_lastFire;
    private long m_lastStaticCollision;
    private boolean m_isColliding;
    private Vector2D m_sparklePosition;
    private boolean m_durt;
    private Vector2D m_lastContactPoint;
    public boolean m_isStaticContact;
    private boolean m_isMyCar;
    private long m_lastTime;
    private int m_random;
    private String m_sound;
}
TOP

Related Classes of com.aqpproject.worldmodel.game.entity.WECar

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.