[fb4fc67] | 1 | package main;
|
---|
| 2 |
|
---|
| 3 | public class Action {
|
---|
| 4 | private long interval;
|
---|
| 5 | public long timeLastPerformed;
|
---|
| 6 | private long timeJustPerformed;
|
---|
| 7 |
|
---|
| 8 | public Action(final long interval) {
|
---|
| 9 | this.interval = interval;
|
---|
| 10 | this.timeLastPerformed = 0L;
|
---|
| 11 | this.timeJustPerformed = 0L;
|
---|
| 12 | }
|
---|
| 13 |
|
---|
| 14 | public Action(final Action copy) {
|
---|
| 15 | this.interval = copy.interval;
|
---|
| 16 | this.timeLastPerformed = 0L;
|
---|
| 17 | this.timeJustPerformed = 0L;
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | public Action copy() {
|
---|
| 21 | return new Action(this);
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | public boolean readyToPerform() {
|
---|
| 25 | if (this.timeJustPerformed == 0L) {
|
---|
| 26 | this.timeJustPerformed = System.currentTimeMillis();
|
---|
| 27 | this.timeLastPerformed = this.timeJustPerformed;
|
---|
| 28 | return true;
|
---|
| 29 | }
|
---|
| 30 | final long timeCurrent = System.currentTimeMillis();
|
---|
| 31 | if (timeCurrent - this.timeJustPerformed >= this.interval) {
|
---|
| 32 | this.timeLastPerformed = this.timeJustPerformed;
|
---|
| 33 | this.timeJustPerformed = timeCurrent;
|
---|
| 34 | return true;
|
---|
| 35 | }
|
---|
| 36 | return false;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | public static class Defend extends Action {
|
---|
| 40 | public Defend(final long interval) {
|
---|
| 41 | super(interval);
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | public Defend(final Defend copy) {
|
---|
| 45 | super(copy);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | @Override
|
---|
| 49 | public Defend copy() {
|
---|
| 50 | return new Defend(this);
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public static class Fire extends Action {
|
---|
| 55 | public Fire(final long interval) {
|
---|
| 56 | super(interval);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | public Fire(final Fire copy) {
|
---|
| 60 | super(copy);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | @Override
|
---|
| 64 | public Fire copy() {
|
---|
| 65 | return new Fire(this);
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | public static class Move extends Action {
|
---|
| 70 | public Move(final long interval) {
|
---|
| 71 | super(interval);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | public Move(final Move copy) {
|
---|
| 75 | super(copy);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | @Override
|
---|
| 79 | public Move copy() {
|
---|
| 80 | return new Move(this);
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | }
|
---|