Add aim control

This commit is contained in:
Victor 2014-02-26 14:26:51 +02:00
parent 19388f2e66
commit 1f9e02be8e
2 changed files with 36 additions and 2 deletions

View File

@ -2,18 +2,24 @@ package com.annimon.militaryhero;
import com.annimon.jecp.ApplicationListener; import com.annimon.jecp.ApplicationListener;
import com.annimon.jecp.Graphics; import com.annimon.jecp.Graphics;
import com.annimon.jecp.Image; import com.annimon.jecp.Jecp;
import com.annimon.jecp.Keys;
/** /**
* @author aNNiMON * @author aNNiMON
*/ */
public class MainApp implements ApplicationListener { public class MainApp extends InputProcessor implements ApplicationListener {
private int width, height; private int width, height;
private SniperAim aim;
public void onStartApp(int width, int height) { public void onStartApp(int width, int height) {
this.width = width; this.width = width;
this.height = height; this.height = height;
aim = new SniperAim(width, height);
Jecp.inputListener = this;
Keys.numericdAsDpad = true;
Keys.wasdAsDpad = true;
} }
public void onPauseApp() { public void onPauseApp() {
@ -23,9 +29,25 @@ public class MainApp implements ApplicationListener {
} }
public void onPaint(Graphics g) { public void onPaint(Graphics g) {
g.setColor(0xFFFFFFFF);
g.fillRect(0, 0, width, height);
aim.draw(g);
} }
public void onUpdate() { public void onUpdate() {
if (isPressed(LEFT)) aim.move(-1, 0);
else if (isPressed(RIGHT)) aim.move(1, 0);
if (isPressed(UP)) aim.move(0, -1);
else if (isPressed(DOWN)) aim.move(0, 1);
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
public void isFirePressed() {
} }
} }

View File

@ -23,6 +23,18 @@ public class SniperAim {
y = (height - aimHeight) / 2; y = (height - aimHeight) / 2;
} }
public void move(int dx, int dy) {
x += dx;
y += dy;
final int aw2 = aimWidth / 2;
if (x < -aw2) x = -aw2;
else if (x + aw2 > width) x = width - aw2;
final int ah2 = aimHeight / 2;
if (y < -ah2) y = -ah2;
else if (y + ah2 > height) y = height - ah2;
}
public int getAimX() { public int getAimX() {
return x + aimWidth / 2; return x + aimWidth / 2;
} }