Skip to content
Snippets Groups Projects
Select Git revision
  • 7e034174322a099f551c4d88f2e73c0aed67e8cf
  • master default
2 results

Movable.java

Blame
  • Forked from LABOUREL Arnaud / Game engine template
    48 commits ahead of the upstream repository.
    user avatar
    bosskkev authored
    4f3658a3
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    Movable.java 2.64 KiB
    package engine.physic;
    
    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;
    
    
        public 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;
                }
            }
    
        }
    
        @Override
        public int getWidth() {
            return width;
        }
    
        @Override
        public int getHeight() {
            return height;
        }
    }