[7d9c033] | 1 | import java.awt.Graphics;
|
---|
| 2 | import java.awt.event.KeyEvent;
|
---|
| 3 | import java.awt.image.BufferedImage;
|
---|
| 4 |
|
---|
| 5 | public class HumanShip extends Ship {
|
---|
| 6 | private boolean upPressed;
|
---|
| 7 | private boolean downPressed;
|
---|
| 8 | private boolean leftPressed;
|
---|
| 9 | private boolean rightPressed;
|
---|
| 10 | private long timeDownPressed;
|
---|
| 11 | private long timeUpPressed;
|
---|
| 12 | private long timeLeftPressed;
|
---|
| 13 | private long timeRightPressed;
|
---|
| 14 | private int startUpPressed;
|
---|
| 15 | private int startDownPressed;
|
---|
| 16 | private int startLeftPressed;
|
---|
| 17 | private int startRightPressed;
|
---|
| 18 |
|
---|
| 19 | public HumanShip(final int newX, final int newY, final int newSpeed, final int newMaxHitpoints, final int newDamage, final int newShotSpeed, final int newShotInterval, final BufferedImage newPic, final BufferedImage newShotPic) {
|
---|
| 20 | super(newX, newY, newSpeed, newMaxHitpoints, 0, newDamage, newShotSpeed, newShotInterval, 0, -25, newPic, newShotPic);
|
---|
| 21 | this.upPressed = false;
|
---|
| 22 | this.downPressed = false;
|
---|
| 23 | this.leftPressed = false;
|
---|
| 24 | this.rightPressed = false;
|
---|
| 25 | this.timeDownPressed = 0L;
|
---|
| 26 | this.timeUpPressed = 0L;
|
---|
| 27 | this.timeLeftPressed = 0L;
|
---|
| 28 | this.timeRightPressed = 0L;
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | public void handleEvent(final KeyEvent e) {
|
---|
| 32 | switch (e.getKeyCode()) {
|
---|
| 33 | case 38: {
|
---|
| 34 | if (!this.upPressed) {
|
---|
| 35 | this.timeUpPressed = System.currentTimeMillis();
|
---|
| 36 | this.startUpPressed = this.getY();
|
---|
| 37 | this.upPressed = true;
|
---|
| 38 | break;
|
---|
| 39 | }
|
---|
| 40 | break;
|
---|
| 41 | }
|
---|
| 42 | case 40: {
|
---|
| 43 | if (!this.downPressed) {
|
---|
| 44 | this.timeDownPressed = System.currentTimeMillis();
|
---|
| 45 | this.startDownPressed = this.getY();
|
---|
| 46 | this.downPressed = true;
|
---|
| 47 | break;
|
---|
| 48 | }
|
---|
| 49 | break;
|
---|
| 50 | }
|
---|
| 51 | case 37: {
|
---|
| 52 | if (!this.leftPressed) {
|
---|
| 53 | this.timeLeftPressed = System.currentTimeMillis();
|
---|
| 54 | this.startLeftPressed = this.getX();
|
---|
| 55 | this.leftPressed = true;
|
---|
| 56 | break;
|
---|
| 57 | }
|
---|
| 58 | break;
|
---|
| 59 | }
|
---|
| 60 | case 39: {
|
---|
| 61 | if (!this.rightPressed) {
|
---|
| 62 | this.timeRightPressed = System.currentTimeMillis();
|
---|
| 63 | this.startRightPressed = this.getX();
|
---|
| 64 | this.rightPressed = true;
|
---|
| 65 | break;
|
---|
| 66 | }
|
---|
| 67 | break;
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | public void draw(final Graphics g) {
|
---|
| 73 | int xChange = 0;
|
---|
| 74 | int yChange = 0;
|
---|
| 75 | if (this.upPressed) {
|
---|
| 76 | yChange -= (int)((System.currentTimeMillis() - this.timeUpPressed) * this.getSpeed() / 1000L);
|
---|
| 77 | }
|
---|
| 78 | if (this.downPressed) {
|
---|
| 79 | yChange += (int)((System.currentTimeMillis() - this.timeDownPressed) * this.getSpeed() / 1000L);
|
---|
| 80 | }
|
---|
| 81 | if (this.leftPressed) {
|
---|
| 82 | xChange -= (int)((System.currentTimeMillis() - this.timeLeftPressed) * this.getSpeed() / 1000L);
|
---|
| 83 | }
|
---|
| 84 | if (this.rightPressed) {
|
---|
| 85 | xChange += (int)((System.currentTimeMillis() - this.timeRightPressed) * this.getSpeed() / 1000L);
|
---|
| 86 | }
|
---|
| 87 | if ((!this.downPressed || this.timeUpPressed < this.timeDownPressed) && this.upPressed) {
|
---|
| 88 | this.setY(this.startUpPressed + yChange);
|
---|
| 89 | }
|
---|
| 90 | else if (this.downPressed) {
|
---|
| 91 | this.setY(this.startDownPressed + yChange);
|
---|
| 92 | }
|
---|
| 93 | if (this.upPressed && this.downPressed) {
|
---|
| 94 | this.timeUpPressed = System.currentTimeMillis();
|
---|
| 95 | this.timeDownPressed = System.currentTimeMillis();
|
---|
| 96 | this.startUpPressed = this.getY();
|
---|
| 97 | this.startDownPressed = this.getY();
|
---|
| 98 | }
|
---|
| 99 | if ((!this.rightPressed || this.timeLeftPressed < this.timeRightPressed) && this.leftPressed) {
|
---|
| 100 | this.setX(this.startLeftPressed + xChange);
|
---|
| 101 | }
|
---|
| 102 | else if (this.rightPressed) {
|
---|
| 103 | this.setX(this.startRightPressed + xChange);
|
---|
| 104 | }
|
---|
| 105 | if (this.getX() < this.getPic().getWidth() / 2) {
|
---|
| 106 | this.setX(this.getPic().getWidth() / 2);
|
---|
| 107 | }
|
---|
| 108 | if (this.getX() > 800 - this.getPic().getWidth() / 2) {
|
---|
| 109 | this.setX(800 - this.getPic().getWidth() / 2);
|
---|
| 110 | }
|
---|
| 111 | if (this.getY() < 0 + this.getPic().getHeight() / 2) {
|
---|
| 112 | this.setY(0 + this.getPic().getHeight() / 2);
|
---|
| 113 | }
|
---|
| 114 | if (this.getY() > 600 - this.getPic().getHeight() / 2) {
|
---|
| 115 | this.setY(600 - this.getPic().getHeight() / 2);
|
---|
| 116 | }
|
---|
| 117 | if (this.leftPressed && this.rightPressed) {
|
---|
| 118 | this.timeLeftPressed = System.currentTimeMillis();
|
---|
| 119 | this.timeRightPressed = System.currentTimeMillis();
|
---|
| 120 | this.startLeftPressed = this.getX();
|
---|
| 121 | this.startRightPressed = this.getX();
|
---|
| 122 | }
|
---|
| 123 | super.draw(g);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | public void reset() {
|
---|
| 127 | super.reset();
|
---|
| 128 | this.upPressed = false;
|
---|
| 129 | this.downPressed = false;
|
---|
| 130 | this.leftPressed = false;
|
---|
| 131 | this.rightPressed = false;
|
---|
| 132 | this.timeDownPressed = 0L;
|
---|
| 133 | this.timeUpPressed = 0L;
|
---|
| 134 | this.timeLeftPressed = 0L;
|
---|
| 135 | this.timeRightPressed = 0L;
|
---|
| 136 | this.startUpPressed = 0;
|
---|
| 137 | this.startDownPressed = 0;
|
---|
| 138 | this.startLeftPressed = 0;
|
---|
| 139 | this.startRightPressed = 0;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | public void setUpPressed(final boolean pressed) {
|
---|
| 143 | this.upPressed = pressed;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | public void setDownPressed(final boolean pressed) {
|
---|
| 147 | this.downPressed = pressed;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | public void setLeftPressed(final boolean pressed) {
|
---|
| 151 | this.leftPressed = pressed;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | public void setRightPressed(final boolean pressed) {
|
---|
| 155 | this.rightPressed = pressed;
|
---|
| 156 | }
|
---|
| 157 | }
|
---|