Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
- import java.util.*;
- /**
- * Write a description of class Tower here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- public class Tower extends Actor
- {
- private long pauseTime;
- public Tower()
- {
- GreenfootImage image = getImage();
- image.scale(image.getWidth() * 2, image.getHeight() * 2);
- pauseTime = 0;
- }
- /**
- * Act - do whatever the Tower wants to do. This method is called whenever
- * the 'Act' or 'Run' button gets pressed in the environment.
- */
- public void act()
- {
- fire();
- }
- public Actor canFire()
- {
- List<Actor> list = getObjectsInRange(100, Enemy.class);
- World world = getWorld();
- if(list.size() == 0)
- {
- return null;
- }
- else
- {
- Actor enemy = list.get(0);
- return enemy;
- }
- }
- public void fire()
- {
- if(running())
- {
- if(canFire() != null)
- {
- getWorld().addObject(new Shot(), getX(),getY());
- }
- }
- pause(300);
- }
- public boolean running()
- {
- long time = System.currentTimeMillis();
- return time > pauseTime;
- }
- public void pause(long delay)
- {
- long time = System.currentTimeMillis();
- pauseTime = time + delay;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement