Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Image;
- import java.awt.Rectangle;
- import javax.swing.ImageIcon;
- public class Entity {
- //-----------------------------------------------------------------
- // Variables
- //-----------------------------------------------------------------
- protected GameCanvas gc;
- protected Sprite sprite;
- protected Image[] image;
- protected Animation a;
- protected boolean firstTime;
- protected Rectangle rect = new Rectangle();
- protected Rectangle drawRect = new Rectangle();
- protected int health;
- protected String imageName;
- protected String entityName;
- protected int initX;
- protected int initY;
- protected boolean moveUp = false;
- protected boolean moveDown = false;
- protected boolean moveLeft = false;
- protected boolean moveRight = false;
- protected int speed = 1;
- public boolean talking = false;
- protected int walking = 0; // 1 = UP, 2 = DOWN, 3 = LEFT, 4 = RIGHT, 0 = FALSE
- protected int facing = 2; // 1 = UP, 2 = DOWN, 3 = LEFT, 4 = RIGHT
- protected int moveTo;
- protected NPC talkableNPC;
- public boolean movableUp = true;
- public boolean movableDown = true;
- public boolean movableLeft = true;
- public boolean movableRight = true;
- //-----------------------------------------------------------------
- // Update Methods
- //-----------------------------------------------------------------
- public void update(long timePassed){
- sprite.update(timePassed);
- rect.setBounds(getDrawX(), getDrawY(), getWidth(), getHeight());
- drawRect.setBounds(getDrawX(), getDrawY(), getWidth(), getHeight());
- if(!gc.debugNoClip){
- checkCollision();
- }
- else{
- movableUp = true;
- movableDown = true;
- movableLeft = true;
- movableRight = true;
- }
- usePortal();
- move();
- }
- public void loadImages(){
- image = new Image[8];
- for(byte i = 0; i < image.length; i++){
- image[i] = new ImageIcon("images\\" + imageName + (i) + ".gif").getImage();
- }
- a = new Animation();
- sprite = new Sprite(a);
- a.addScene(image[1], 150);
- sprite.setCoordX(initX); //Sets initial xPos
- sprite.setCoordY(initY); //Sets initial yPos
- }
- //-----------------------------------------------------------------
- // Collision Methods
- //-----------------------------------------------------------------
- public boolean crashTestNPCUp() {
- for (int i = 0; i < gc.currentMapNPC.length; i++) {
- Entity him = gc.currentMapNPC[i];
- if(getCoordY() == him.getCoordY() + 1 && getCoordX() == him.getCoordX()){
- if(facing == 1){
- talkableNPC = (NPC) him;
- }
- return true;
- }
- }
- return false;
- }
- public boolean crashTestTileUp(){
- for(int i = 0; i < gc.mapTileLayer1.length; i++){
- StaticTile tile = gc.mapTileLayer1[i];
- if(gc.checkCollisionBuffer(tile.getCoordX(), tile.getCoordY()) == true){
- if(tile.getImpassible() == true){
- if(getCoordY() == tile.getCoordY() + 1 && getCoordX() == tile.getCoordX()){
- return true;
- }
- }
- }
- }
- for(int i = 0; i < gc.mapTileLayer2.length; i++){
- StaticTile tile = gc.mapTileLayer2[i];
- if(gc.checkCollisionBuffer(tile.getCoordX(), tile.getCoordY()) == true){
- if(tile.getImpassible() == true){
- if(getCoordY() == tile.getCoordY() + 1 && getCoordX() == tile.getCoordX()){
- return true;
- }
- }
- }
- }
- return false;
- }
- public boolean crashTestNPCDown(){
- for (int i = 0; i < gc.currentMapNPC.length; i++) {
- Entity him = gc.currentMapNPC[i];
- if(getCoordY() + 1 == him.getCoordY() && getCoordX() == him.getCoordX()){
- if(facing == 2){
- talkableNPC = (NPC) him;
- }
- return true;
- }
- }
- return false;
- }
- public boolean crashTestTileDown(){
- for(int i = 0; i < gc.mapTileLayer1.length; i++){
- StaticTile tile = gc.mapTileLayer1[i];
- if(gc.checkCollisionBuffer(tile.getCoordX(), tile.getCoordY()) == true){
- if(tile.getImpassible() == true){
- if(getCoordY() + 1 == tile.getCoordY() && getCoordX() == tile.getCoordX()){
- return true;
- }
- }
- }
- }
- for(int i = 0; i < gc.mapTileLayer2.length; i++){
- StaticTile tile = gc.mapTileLayer2[i];
- if(gc.checkCollisionBuffer(tile.getCoordX(), tile.getCoordY()) == true){
- if(tile.getImpassible() == true){
- if(getCoordY() + 1 == tile.getCoordY() && getCoordX() == tile.getCoordX()){
- return true;
- }
- }
- }
- }
- return false;
- }
- public boolean crashTestNPCLeft(){
- for (int i = 0; i < gc.currentMapNPC.length; i++) {
- Entity him = gc.currentMapNPC[i];
- if(getCoordX() == him.getCoordX() + 1 && getCoordY() == him.getCoordY()){
- if(facing == 3){
- talkableNPC = (NPC) him;
- }
- return true;
- }
- }
- return false;
- }
- public boolean crashTestTileLeft(){
- for(int i = 0; i < gc.mapTileLayer1.length; i++){
- StaticTile tile = gc.mapTileLayer1[i];
- if(gc.checkCollisionBuffer(tile.getCoordX(), tile.getCoordY()) == true){
- if(tile.getImpassible() == true){
- if(getCoordX() == tile.getCoordX() + 1 && getCoordY() == tile.getCoordY()){
- return true;
- }
- }
- }
- }
- for(int i = 0; i < gc.mapTileLayer2.length; i++){
- StaticTile tile = gc.mapTileLayer2[i];
- if(gc.checkCollisionBuffer(tile.getCoordX(), tile.getCoordY()) == true){
- if(tile.getImpassible() == true){
- if(getCoordX() == tile.getCoordX() + 1 && getCoordY() == tile.getCoordY()){
- return true;
- }
- }
- }
- }
- return false;
- }
- public boolean crashTestNPCRight(){
- for (int i = 0; i < gc.currentMapNPC.length; i++) {
- Entity him = gc.currentMapNPC[i];
- if(getCoordX() + 1 == him.getCoordX() && getCoordY() == him.getCoordY()){
- if(facing == 4){
- talkableNPC = (NPC) him;
- }
- return true;
- }
- }
- return false;
- }
- public boolean crashTestTileRight(){
- for(int i = 0; i < gc.mapTileLayer1.length; i++){
- StaticTile tile = gc.mapTileLayer1[i];
- if(gc.checkCollisionBuffer(tile.getCoordX(), tile.getCoordY()) == true){
- if(tile.getImpassible() == true){
- if(getCoordX() + 1 == tile.getCoordX() && getCoordY() == tile.getCoordY()){
- return true;
- }
- }
- }
- }
- for(int i = 0; i < gc.mapTileLayer2.length; i++){
- StaticTile tile = gc.mapTileLayer2[i];
- if(gc.checkCollisionBuffer(tile.getCoordX(), tile.getCoordY()) == true){
- if(tile.getImpassible() == true){
- if(getCoordX() + 1 == tile.getCoordX() && getCoordY() == tile.getCoordY()){
- return true;
- }
- }
- }
- }
- return false;
- }
- public void checkCollision(){
- if(crashTestNPCUp() == true || crashTestTileUp() == true){
- movableUp = false;
- }
- else{
- movableUp = true;
- }
- if(crashTestNPCDown() == true || crashTestTileDown() == true){
- movableDown = false;
- }
- else{
- movableDown = true;
- }
- if(crashTestNPCLeft() == true || crashTestTileLeft() == true){
- movableLeft = false;
- }
- else{
- movableLeft = true;
- }
- if(crashTestNPCRight() == true || crashTestTileRight() == true){
- movableRight = false;
- }
- else{
- movableRight = true;
- }
- }
- //-----------------------------------------------------------------
- // Movement Methods
- //-----------------------------------------------------------------
- public void move(){
- if(moveUp == true && walking == 0 && talking == false){
- facing = 1;
- if(movableUp == true){
- moveTo = (int) Math.round(getCoordY() - 1);
- walking = 1;
- sprite.setVelocityY(-speed);
- }
- }
- else{
- if(walking == 1 && moveTo >= getCoordY()){
- sprite.setVelocityY(0);
- walking = 0;
- }
- }
- if(moveDown == true && walking == 0 && talking == false){
- facing = 2;
- if(movableDown == true){
- moveTo = (int) Math.round(getCoordY() + 1);
- walking = 2;
- sprite.setVelocityY(speed);
- }
- }
- else{
- if(walking == 2 && moveTo <= getCoordY()){
- sprite.setVelocityY(0);
- walking = 0;
- }
- }
- if(moveLeft == true && walking == 0 && talking == false){
- facing = 3;
- if(movableLeft == true){
- moveTo = (int) Math.round(getCoordX() - 1);
- walking = 3;
- sprite.setVelocityX(-speed);
- }
- }
- else{
- if(walking == 3 && moveTo >= getCoordX()){
- sprite.setVelocityX(0);
- walking = 0;
- }
- }
- if(moveRight == true && walking == 0 && talking == false){
- facing = 4;
- if(movableRight == true){
- moveTo = (int) Math.round(getCoordX() + 1);
- walking = 4;
- sprite.setVelocityX(speed);
- }
- }
- else{
- if(walking == 4 && moveTo <= getCoordX()){
- sprite.setVelocityX(0);
- walking = 0;
- }
- }
- }
- public void animate(String direction){
- if(direction.equalsIgnoreCase("up")){
- if(firstTime == true){
- a.removeAllScenes();
- for(byte i = 3; i >= 2; i--){
- a.addScene(image[i], 250);
- }
- firstTime = false;
- }
- }
- }
- public void stopAnimate(String direction){
- if(direction.equalsIgnoreCase("up")){
- firstTime = true;
- a.removeAllScenes();
- a.addScene(image[2], 150);
- }
- }
- public void usePortal(){
- int[][] array = gc.portals;
- for(int i = 0; i < array.length; i++){
- int[] nestedArray = array[i];
- int x1 = nestedArray[0];
- int y1 = nestedArray[1];
- int x2 = nestedArray[2];
- int y2 = nestedArray[3];
- if(getCoordX() == x1 && getCoordY() == y1){
- walking = 0;
- moveUp = false;
- moveDown = false;
- moveLeft = false;
- moveRight = false;
- sprite.setVelocityX(0);
- sprite.setVelocityY(0);
- setCoordX(x2);
- setCoordY(y2);
- }
- }
- }
- //-----------------------------------------------------------------
- // Getter Methods
- //-----------------------------------------------------------------
- public Image getImage(){
- return a.getImage();
- }
- public String getName(){
- return entityName;
- }
- public int getX(){
- return sprite.getX();
- }
- public int getY(){
- return sprite.getY();
- }
- public float getCoordX(){
- return sprite.getCoordX();
- }
- public float getCoordY(){
- return sprite.getCoordY();
- }
- public int getDrawX(){
- return sprite.getX() - gc.offsetX;
- }
- public int getDrawY(){
- return sprite.getY() - gc.offsetY;
- }
- public int getWidth(){
- return sprite.getWidth();
- }
- public int getHeight(){
- return sprite.getHeight();
- }
- public Rectangle getBounds(){
- return rect;
- }
- //-----------------------------------------------------------------
- // Setter Methods
- //-----------------------------------------------------------------
- public void setCoordX(int x){
- sprite.setCoordX(x);
- }
- public void setCoordY(int y){
- sprite.setCoordY(y);
- }
- public void setSpeed(int speed){
- this.speed = speed;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment