Currently showing ../../collision/tilemaps/src/org/newdawn/tilemap/Entity.java Line Numbers: [show|hide]

 ``` 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``` ```package org.newdawn.tilemap; import java.awt.Graphics2D; import java.awt.Image; /** * A single entity moving round our map. It maintains its position * in terms of grid cells. Note that the positions are floating * point numbers allowing us to be partially across a cell. * * @author Kevin Glass */ public class Entity { /** The x position of this entity in terms of grid cells */ private float x; /** The y position of this entity in terms of grid cells */ private float y; /** The image to draw for this entity */ private Image image; /** The map which this entity is wandering around */ private Map map; /** The angle to draw this entity at */ private float ang; /** The size of this entity, this is used to calculate collisions with walls */ private float size = 0.3f; /** * Create a new entity in the game * * @param image The image to represent this entity (needs to be 32x32) * @param map The map this entity is going to wander around * @param x The initial x position of this entity in grid cells * @param y The initial y position of this entity in grid cells */ public Entity(Image image, Map map, float x, float y) { this.image = image; this.map = map; this.x = x; this.y = y; } /** * Move this entity a given amount. This may or may not succeed depending * on collisions * * @param dx The amount to move on the x axis * @param dy The amount to move on the y axis * @return True if the move succeeded */ public boolean move(float dx, float dy) { // work out what the new position of this entity will be float nx = x + dx; float ny = y + dy; // check if the new position of the entity collides with // anything if (validLocation(nx, ny)) { // if it doesn't then change our position to the new position x = nx; y = ny; // and calculate the angle we're facing based on our last move ang = (float) (Math.atan2(dy, dx) - (Math.PI / 2)); return true; } // if it wasn'n't do anything apart from // tell the caller return false; } /** * Check if the entity would be at a valid location if its position * was as specified * * @param nx The potential x position for the entity * @param ny The potential y position for the entity * @return True if the new position specified would be valid */ public boolean validLocation(float nx, float ny) { // here we're going to check some points at the corners of // the player to see whether we're at an invalid location // if any of them are blocked then the location specified // isn't valid if (map.blocked(nx - size, ny - size)) { return false; } if (map.blocked(nx + size, ny - size)) { return false; } if (map.blocked(nx - size, ny + size)) { return false; } if (map.blocked(nx + size, ny + size)) { return false; } // if all the points checked are unblocked then we're in an ok // location return true; } /** * Draw this entity to the graphics context provided. * * @param g The graphics context to which the entity should be drawn */ public void paint(Graphics2D g) { // work out the screen position of the entity based on the // x/y position and the size that tiles are being rendered at. So // if we'e'd render on screen // at 15,15. int xp = (int) (Map.TILE_SIZE * x); int yp = (int) (Map.TILE_SIZE * y); // rotate the sprite based on the current angle and then // draw it g.rotate(ang, xp, yp); g.drawImage(image, (int) (xp - 16), (int) (yp - 16), null); g.rotate(-ang, xp, yp); } } ```

 Total 123 Lines of Code. Source code formatted using showsrc by William Denniss