``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149``` ```package org.newdawn.spaceinvaders; import java.awt.Graphics; import java.awt.Rectangle; /** * An entity represents any element that appears in the game. The * entity is responsible for resolving collisions and movement * based on a set of properties defined either by subclass or externally. * * Note that doubles are used for positions. This may seem strange * given that pixels locations are integers. However, using double means * that an entity can move a partial pixel. It doesn't of course mean that * they will be display half way through a pixel but allows us not lose * accuracy as we move. * * @author Kevin Glass */ public abstract class Entity { /** The current x location of this entity */ protected double x; /** The current y location of this entity */ protected double y; /** The sprite that represents this entity */ protected Sprite sprite; /** The current speed of this entity horizontally (pixels/sec) */ protected double dx; /** The current speed of this entity vertically (pixels/sec) */ protected double dy; /** The rectangle used for this entity during collisions resolution */ private Rectangle me = new Rectangle(); /** The rectangle used for other entities during collision resolution */ private Rectangle him = new Rectangle(); /** * Construct a entity based on a sprite image and a location. * * @param ref The reference to the image to be displayed for this entity * @param x The initial x location of this entity * @param y The initial y location of this entity */ public Entity(String ref,int x,int y) { this.sprite = SpriteStore.get().getSprite(ref); this.x = x; this.y = y; } /** * Request that this entity move itself based on a certain ammount * of time passing. * * @param delta The ammount of time that has passed in milliseconds */ public void move(long delta) { // update the location of the entity based on move speeds x += (delta * dx) / 1000; y += (delta * dy) / 1000; } /** * Set the horizontal speed of this entity * * @param dx The horizontal speed of this entity (pixels/sec) */ public void setHorizontalMovement(double dx) { this.dx = dx; } /** * Set the vertical speed of this entity * * @param dx The vertical speed of this entity (pixels/sec) */ public void setVerticalMovement(double dy) { this.dy = dy; } /** * Get the horizontal speed of this entity * * @return The horizontal speed of this entity (pixels/sec) */ public double getHorizontalMovement() { return dx; } /** * Get the vertical speed of this entity * * @return The vertical speed of this entity (pixels/sec) */ public double getVerticalMovement() { return dy; } /** * Draw this entity to the graphics context provided * * @param g The graphics context on which to draw */ public void draw(Graphics g) { sprite.draw(g,(int) x,(int) y); } /** * Do the logic associated with this entity. This method * will be called periodically based on game events */ public void doLogic() { } /** * Get the x location of this entity * * @return The x location of this entity */ public int getX() { return (int) x; } /** * Get the y location of this entity * * @return The y location of this entity */ public int getY() { return (int) y; } /** * Check if this entity collised with another. * * @param other The other entity to check collision against * @return True if the entities collide with each other */ public boolean collidesWith(Entity other) { me.setBounds((int) x,(int) y,sprite.getWidth(),sprite.getHeight()); him.setBounds((int) other.x,(int) other.y,other.sprite.getWidth(),other.sprite.getHeight()); return me.intersects(him); } /** * Notification that this entity collided with another. * * @param other The entity with which this entity collided. */ public abstract void collidedWith(Entity other); }```