Skip to content
Snippets Groups Projects
Select Git revision
  • 64119975937c9bc2e4908edb7a06389c091ee248
  • main default protected
  • variant
3 results

ModelElement.java

Blame
  • Forked from LABOUREL Arnaud / Firefighter template
    Source project has a limited visibility.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    Movable.java 2.49 KiB
    package engine;
    
    import pong.PongApp;
    
    public class Movable implements Entity {
        private int weight;
    
        private int width;
        private int height;
        private Coordinates2D speed;
        private Coordinates2D position;
    
        private Coordinates2D acceleration;
    
    
        Movable(int weight, Coordinates2D position, Coordinates2D speed, Coordinates2D acceleration, int width, int height) {
            this.weight = weight;
            this.speed = speed;
            this.position = position;
            this.acceleration = acceleration;
            this.width = width;
            this.height = height;
        }
    
        @Override
        public int getWeight() {
            return weight;
        }
    
        @Override
        public Coordinates2D getSpeed() {
            return speed;
        }
    
        @Override
        public Coordinates2D getPosition() {
            return position;
        }
    
        @Override
        public Coordinates2D getAcceleration() {
            return acceleration;
        }
    
        @Override
        public void setSpeed(Coordinates2D speed) {
            this.speed = speed;
        }
    
        @Override
        public void setPosition(Coordinates2D position) {
            this.position = position;
        }
    
        @Override
        public void setAcceleration(Coordinates2D acceleration) {
    
        }
    
        @Override
        public void updatePosition() {
            Coordinates2D newPosition = new Coordinates2D(this.position.getX(), this.position.getY());
            newPosition.add(this.speed);
    
            if (newPosition.getX() < 0 || newPosition.getX() >= PongApp.width - this.width) {
                this.speed = new Coordinates2D(-speed.getX(), speed.getY());
            }
            if (newPosition.getY() < 0 || newPosition.getY() >= PongApp.height - this.height -20) { // -20 à cause de la barre verticale de la fenêtre
                this.speed = new Coordinates2D(speed.getX(), -speed.getY());
            }
            this.position.add(this.speed); // Modifying the position (x = x + vx ; y = y + vy)
        }
    
        @Override
        public int getLeftResistance() {
            return 0;
        }
    
        @Override
        public void setResistance(int leftLife) {
    
        }
    
        @Override
        public void halt() {
            this.speed = new Coordinates2D(0, 0); // Making the speed null
            updatePosition(); // Updating the position with a null acceleration
        }
    
        public void setDirection(Movable movable, Coordinates2D direction) throws InterruptedException {
            while (true) {
                this.speed = direction;
                this.position.add(this.speed);
                if (movable.getSpeed() != direction) {
                    break;
                }
            }
    
        }
    }