import java.awt.Graphics; import java.awt.Color; public class Tank extends MobileSimObject{ // Overview: Tank is a MobileSimObject that can shoot shells to destroy // target tanks or (destroyable) blocks. protected int speed; //the lower the faster(use with delay); protected Direction orient; protected Gun gun; protected Item[] items = new Item[3]; protected boolean shooting; protected static Status status = new Status(); //contains score, xp and numEnemies for the game. static int numEnemies=1; public Tank(){ this.hp = 1; this.speed = 500; this.orient = Direction.NORTH; this.gun = new Gun(); this.shooting=false; } public Tank(int hp, Direction or, int speed){ this.hp = hp; this.speed = speed; this.orient = or; this.gun = new Gun(); this.shooting=false; } /*******MODIFIED********/ synchronized public void die() { //Modifies: this //Effects: remove this from the grid setDead(true); TankSimulator.death.play(); grid.removeObjectAt(getX(), getY()); } /***********************/ public void setGun(Gun gun){ this.gun = gun; } public void increaseStatus(int score, int xp){ status.increaseStatus(score,xp); } public Status getStatus(){ return status; } public void decreaseNumEnemies(){ synchronized(getGrid()){ numEnemies--; } } public void setNumEnemies(int n){ numEnemies = n; } public void shoot(){ //Modifies: Shell object //Effects: create a Shell object and shoot it in the current orient Direction. shooting = true; //shooting'll turn back to false in executeRun() synchronized(getGrid()){ int newx = getX()+orient.easterlyDirection(); int newy = getY()-orient.northerlyDirection(); if(getGrid().validLocation(newx,newy)){ SimObject so = getGrid().getObjectAt(newx,newy); if(so != null){ so.decreaseHP(gun.getDamage()); } else { Shell bullet = gun.getBullet(orient); if(this instanceof UserTank){ bullet.setColor(Color.red); } try{ getGrid().setObjectAt(newx,newy,bullet); bullet.init(newx,newy,getGrid()); TankSimulator.gun.play(); bullet.resumeObject(); }catch(BadLocationException e){ System.err.println("BadLocation for shell"); } } } } } public void setSpeed(int newSpeed){ //Modifies: this //Effects: set the speed of this this.speed = newSpeed; } synchronized public void moveDir(Direction dir){ //Modifies: this //Effects: move this one block in dir Direction. if(hp<=0) return; synchronized(getGrid()){ if(dir.equals(orient)){ int newx = getX()+dir.easterlyDirection(); int newy = getY()-dir.northerlyDirection(); if (getGrid().validLocation(newx, newy)) { if (getGrid().isSquareEmpty(newx, newy)) { //@nowarn Pre setLocation(newx,newy); }else { SimObject so = getGrid().getObjectAt(newx,newy); if(so instanceof Item){ takeItem((Item)so); setLocation(newx,newy); }else if (so instanceof Abyss) { Abyss hole = (Abyss) so; hole.swallow(this); } } } }else{ orient = dir; } } } public void takeItem(Item it){ getGrid().removeObjectAt(it.getX(),it.getY()); if(items[0]==null){ items[0]=it; } else if(items[1]==null){ items[1]=it; } else if(items[2]==null){ items[2]=it; } else{ System.out.println("Inventory is full."); } } public void setItem(Item it, int i){ if(i>=0&&i<3){ items[i]=it; } } public void useItem(int i){ if(i>0&&i<=3){ Item tmp = items[i-1]; if(tmp!=null){ if(!tmp.permanent){ items[i-1]=null; } tmp.action(this); } } } public Item getItem(int i){ if(i<0 || i>2) return null; if(items[i]!=null){ return items[i]; } return null; } public void drawImage(Graphics g, int x, int y, int w, int h){ g.setColor(getColor()); g.fillRect(x+w/5,y+h/5,3*w/5-1,3*h/5-1); //draw the hp g.setColor(Color.green); for(int i=1;i<=hp;i++){ g.fillRect(x+3*i-2,y+h-5,2,4); } drawGun(g,x,y,w,h); } public void drawGun(Graphics g, int x, int y, int w, int h){ //Effects: draw the gun according to the orientation g.setColor(Color.black); int midx = x + w/2; int midy = y + h/2; if(orient.equals(Direction.NORTH)){ g.fillRect(midx-3,y,4,h/2); } else if(orient.equals(Direction.SOUTH)){ g.fillRect(midx-3,midy-1,4,h/2); } else if(orient.equals(Direction.WEST)){ g.fillRect(x,midy-3,w/2,4); } else if(orient.equals(Direction.EAST)){ g.fillRect(midx-1,midy-3,w/2,4); } } public Color getColor(){ return Color.yellow; } }