Untitled
unknown
plain_text
3 years ago
14 kB
9
Indexable
public class Mob extends greenfoot.Actor { /** turn and move */ GifImage gifImage = new GifImage("ghost.gif"); int ghostHealth = 5,count = 10,sx = 15,sy = 15; int direction = 0;// 0 = right; 1 = down ; 2 = left ; 3 = up // 0 = right; 1 = down ; 2 = left ; 3 = up boolean hitByBullet = false; boolean remove = false; final int cooldown_reset = 90; int cooldown_cnt = cooldown_reset; int move_speed = 1; private int abs(int a) { if (a <= 0) return -a; else return a; } private boolean ninja_nearby(int x, int y, int distance) { return (abs(x*x+y*y-getX()*getX()-getY()*getY()) <= distance); } private void shoot(int direction) { getWorld().addObject(new Salt(90 * direction), getX(), getY()); } public void detectNinja(int i) { ghostHealth = ghostHealth + i; if(ghostHealth<=0) remove = true; } private boolean detectWallY(int x,int y, int dy) { if( getOneObjectAtOffset(0, dy*sy, Wall.class)== null && getOneObjectAtOffset(17, dy*sy, Wall.class)== null && getOneObjectAtOffset(-16, dy*sy, Wall.class)== null) { return false; //if (!(getY()+dy < getImage().getHeight()/2 || getY()+dy > getWorld().getHeight()-getImage().getHeight()/2)) return false; } return true; } private boolean detectWallX(int x,int y,int dx) { if( getOneObjectAtOffset(dx*sx, 0, Wall.class)== null && getOneObjectAtOffset(dx*sx, 19, Wall.class)== null && getOneObjectAtOffset(dx*sx, -10, Wall.class)== null) { return false; //if (!(getX()+dx < getImage().getWidth()/2 || getX()+dx > getWorld().getWidth()-getImage().getWidth()/2)) return false; } return true; } private boolean detectBullet() { int ssx = 10; int ssy = 10; if( getOneObjectAtOffset(0, ssy, Bullet.class)== null && getOneObjectAtOffset(17, ssy, Bullet.class)== null && getOneObjectAtOffset(-16, ssy, Bullet.class)== null) return false; if( getOneObjectAtOffset(ssx, 0, Bullet.class)== null && getOneObjectAtOffset(ssx, 19, Bullet.class)== null && getOneObjectAtOffset(ssx, -10, Bullet.class)== null) return false; else return true; } public void HitByBullet(int i) { ghostHealth = ghostHealth + i; hitByBullet = true; if(ghostHealth<=0) remove = true; } public boolean TouchActor() { int ssx = 8; int ssy = 8; if(getOneObjectAtOffset(0, ssy, Ninja.class)== null && getOneObjectAtOffset(17, ssy, Ninja.class)== null && getOneObjectAtOffset(-16, ssy, Ninja.class)== null) return false; if(getOneObjectAtOffset(ssx, 0, Ninja.class)== null && getOneObjectAtOffset(ssx, 19, Ninja.class)== null && getOneObjectAtOffset(ssx, -10, Ninja.class)== null) return false; else return true; } private void random_move(){ int rand = greenfoot.Greenfoot.getRandomNumber(500); if (rand == 1) direction += 1; else if(rand == 2) direction -= 1; else if (rand == 3) direction -= 2; while (direction > 3) direction -= 4; while (direction < 0) direction += 4; int dx = 0, dy = 0; int stuck = 0; for (int i = 0; i < 1; i++) { switch(direction) { case 0: if (detectWallX(getX(), getY(), 1)) { direction += 1; i += 1; stuck += 1; } else {dx = 1; dy = 0;} break; case 1: if (detectWallY(getX(), getY(), 1)) { direction += 1; i += 1; stuck += 1; } else {dx = 0; dy = 1;} break; case 2: if (detectWallX(getX(), getY(), -1)) { direction += 1; i += 1; stuck += 1; } else {dx = -1; dy = 0;} break; case 3: if (detectWallY(getX(), getY(), -1)) { direction = 0; i += 1; stuck += 1; } else {dx = 0; dy = -1;} break; default: return; } if (stuck > 4) return; } //move setLocation(getX()+dx, getY()+dy); } private void nearby_move(boolean move){ int x = ((Welt)getWorld()).getNinjaX() - getX(); int y = ((Welt)getWorld()).getNinjaY() - getY(); int list[] = {0, 0, 0, 0}; // 0 = right; 1 = down ; 2 = left ; 3 = up // 0 = right; 1 = down ; 2 = left ; 3 = up if (abs(x) <= 26) x = 0; if (abs(y) <= 25) y = 0; if (x == 0){ if (y > 0) y = -1; else y = 1; list[0] = 2 + y; list[1] = 2; list[2] = 0; list[3] = 2 - y; } else if (y == 0){ if (x < 0) x = -1; else x = 1; list[0] = 1 - x; list[1] = 3; list[2] = 1; list[3] = 1 + x; } else if(abs(x) < abs(y)){ if (x < 0) x = -1; else x = 1; if (y > 0) y = -1; else y = 1; list[0] = 1 - x; list[1] = 2 + y; list[2] = 2 - y; list[3] = 1 + x; } else { if (x < 0) x = -1; else x = 1; if (y > 0) y = -1; else y = 1; list[0] = 2 + y; list[1] = 1 - x; list[2] = 1 + x; list[3] = 2 - y; } int dx = 0, dy = 0; int stuck = 0; int c = 0; for (int i = 0; i < 1; i++) { switch(list[stuck]) { case 0: if (detectWallX(getX(), getY(), 1)) { direction += 1; i -= 1; stuck += 1; } else {direction = 0;dx = 1; dy = 0;} break; case 1: if (detectWallY(getX(), getY(), 1)) { direction += 1; i -= 1; stuck += 1; } else {direction = 1;dx = 0; dy = 1;} break; case 2: if (detectWallX(getX(), getY(), -1)) { direction += 1; i -= 1; stuck += 1; } else {direction = 2;dx = -1; dy = 0;} break; case 3: if (detectWallY(getX(), getY(), -1)) { direction = 0; i -= 1; stuck += 1; } else {direction = 3;dx = 0; dy = -1;} break; default: return; } if (stuck >= 4) return; } //move if (move){ setLocation(getX()+dx, getY()+dy); } } private void direction_correction(){ int x = ((Welt)getWorld()).getNinjaX() - getX(); int y = ((Welt)getWorld()).getNinjaY() - getY(); int x2 = ((Welt)getWorld()).getNinjaX() - getX(); int y2 = ((Welt)getWorld()).getNinjaY() - getY(); int list[] = {0, 0, 0, 0}; // 0 = right; 1 = down ; 2 = left ; 3 = up // 0 = right; 1 = down ; 2 = left ; 3 = up if (abs(x) <= 26) x = 0; if (abs(y) <= 25) y = 0; if (x == 0 && y == 0){ if (abs(x2) > abs(y2)){ x = x2; y = 0; } else{ x = 0; y = y2; } } if (x == 0){ if (y > 0) y = -1; else y = 1; list[0] = 2 + y; list[1] = 2; list[2] = 0; list[3] = 2 - y; } else if (y == 0){ if (x < 0) x = -1; else x = 1; list[0] = 1 - x; list[1] = 3; list[2] = 1; list[3] = 1 + x; } else if(abs(x) < abs(y)){ if (x < 0) x = -1; else x = 1; if (y > 0) y = -1; else y = 1; list[0] = 1 - x; list[1] = 2 + y; list[2] = 2 - y; list[3] = 1 + x; } else { if (x < 0) x = -1; else x = 1; if (y > 0) y = -1; else y = 1; list[0] = 2 + y; list[1] = 1 - x; list[2] = 1 + x; list[3] = 2 - y; } int dx = 0, dy = 0; int stuck = 0; int c = 0; for (int i = 0; i < 1; i++) { switch(list[stuck]) { case 0: if (detectWallX(getX(), getY(), 1)) { random_move(); } else {direction = 0;dx = 1; dy = 0;} break; case 1: if (detectWallY(getX(), getY(), 1)) { random_move(); } else {direction = 1;dx = 0; dy = 1;} break; case 2: if (detectWallX(getX(), getY(), -1)) { random_move(); } else {direction = 2;dx = -1; dy = 0;} break; case 3: if (detectWallY(getX(), getY(), -1)) { random_move(); } else {direction = 3;dx = 0; dy = -1;} break; default: return; } } } public boolean in_stop_moving_zone(){ int x = abs(((Welt)getWorld()).getNinjaX() - getX()); int y = abs(((Welt)getWorld()).getNinjaY() - getY()); return (x <= 50 && y <= 50); } public boolean in_reverse_moving_zone(){ int x = abs(((Welt)getWorld()).getNinjaX() - getX()); int y = abs(((Welt)getWorld()).getNinjaY() - getY()); return (x <= 45 && y <= 45); } public void reverse_move(){ int x = ((Welt)getWorld()).getNinjaX() - getX(); int y = ((Welt)getWorld()).getNinjaY() - getY(); boolean bool1 = x>y; boolean bool2 = x+y>0; if ((!bool1) && (bool2)){ //up if (detectWallY(getX(), getY(), -1)) random_move(); else setLocation(getX(), getY()-1); } else if ((bool1) && (bool2)){ //left if (detectWallX(getX(), getY(), -1)) random_move(); else setLocation(getX()-1, getY()); } else if ((!bool1) && (!bool2)){ //right if (detectWallX(getX(), getY(), 1)) random_move(); else setLocation(getX()+1, getY()); } else{ //down if (detectWallY(getX(), getY(), 1)) random_move(); else setLocation(getX(), getY()+1); } } boolean invisible; public void DetectNinjaInvisible() { Ninja a = ((Welt)getWorld()).getNinja(); if(a!= null) { invisible = a.NinjaIsInvisible(); } } public void act() { DetectNinjaInvisible(); if(hitByBullet) { if(count <= 10) { setImage("ghost.png"); count--; } if(count<0){ setImage(gifImage.getCurrentImage()); count = 10; hitByBullet = false; } } else setImage(gifImage.getCurrentImage()); //move for(int i =0; i < move_speed;i++){ if (!invisible && ninja_nearby(((Welt)getWorld()).getNinjaX(), ((Welt)getWorld()).getNinjaY(),200000)){ //nearby if(in_reverse_moving_zone()) reverse_move(); else if(in_stop_moving_zone()) direction_correction(); else nearby_move(true); } else random_move(); } //attack if (cooldown_cnt >= 0) cooldown_cnt -= 2; if(!invisible && ninja_nearby(((Welt)getWorld()).getNinjaX(), ((Welt)getWorld()).getNinjaY(),100000)) { if (cooldown_cnt <= 0) { shoot(direction); cooldown_cnt = cooldown_reset; } } if(remove == true) getWorld().removeObject(this); } }
Editor is loading...