Java Examples for worm.entities.Capacitor
The following java examples will help you to understand the usage of worm.entities.Capacitor. These source code samples are taken from different open source projects.
Example 1
| Project: titans-master File: WormGameState.java View source code |
/**
* Check for clicking and set the mouse pointer to the right animation for whatever's going on
*/
private void checkMouse() {
if (GameScreen.getInstance().isBlocked()) {
return;
}
if (GameScreen.isSomethingElseGrabbingMouse()) {
return;
}
// Ignore mouse on active areas, unless grabbed
int mouseX = GameScreen.getInstance().getMouseX();
int mouseY = GameScreen.getInstance().getMouseY();
List<Area> areasUnderMouse = GameScreen.getInstance().getAreasUnder(mouseX, mouseY);
if (GameScreen.getInstance().getGrabbed() == null) {
for (Area area : areasUnderMouse) {
if (area.isFocusable()) {
if (buildEntity != null) {
buildEntity.setVisible(false);
}
Worm.setMouseAppearance(Res.getMousePointer());
return;
}
}
}
if (buildEntity != null && !buildEntity.isVisible()) {
buildEntity.setVisible(true);
}
float x = mouseX - GameScreen.getSpriteOffset().getX();
float y = mouseY - GameScreen.getSpriteOffset().getY();
int n = entities.size();
Entity hovered = null;
if (clicked == null) {
clicked = new ArrayList<Entity>(8);
} else {
clicked.clear();
}
for (int i = 0; i < n; i++) {
Entity entity = entities.get(i);
if (entity.isTouching(x, y)) {
if (entity.isClickable()) {
clicked.add(entity);
hovered = entity;
} else if (hovered == null && entity.isHoverable()) {
hovered = entity;
}
}
}
// Check range of capacitors
capacitorRange = false;
TEMP_CAPACITORS.clear();
for (int i = 0; i < buildings.size(); i++) {
Building b = buildings.get(i);
if (b.isAlive() && b instanceof Capacitor) {
Capacitor capacitor = (Capacitor) b;
if (b.getDistanceTo(x, y) <= capacitor.getZapRadius()) {
capacitorRange = true;
TEMP_CAPACITORS.add(capacitor);
}
}
}
if (mode == Mode.MODE_BUILD) {
buildEntity.setLocation(quantize(x - building.getBounds().getX() - building.getBounds().getWidth() / 2 + MAP_RESOLUTION / 2), quantize(y - building.getBounds().getY() - building.getBounds().getHeight() / 2 + MAP_RESOLUTION / 2));
}
boolean auxDown = false;
int count = Mouse.getButtonCount();
for (int i = 2; i < count; i++) {
if (Mouse.isButtonDown(i)) {
auxDown = true;
break;
}
}
if (Mouse.isButtonDown(0)) {
if (waitForMouse) {
return;
}
GameScreen.onMapGrabbedMouse(true);
switch(mode) {
case Mode.MODE_SELL:
case Mode.MODE_NORMAL:
case Mode.MODE_BUILD:
case Mode.MODE_DRAG:
// we enter zap mode. If we're outside capacitor range, we set "no click" mode.
if (clicked.size() > 0) {
if (leftMouseWasDown) {
// But we're in "no click" mode...
return;
}
// Ok, enter no click mode
if (mode != Mode.MODE_DRAG) {
leftMouseWasDown = true;
}
// Hovering over something. Pick the first one and let that decide what mouse pointer to use.
Worm.setMouseAppearance(clicked.get(0).getMousePointer(true));
boolean fire = false, maybeSelect = false;
for (Iterator<Entity> i = clicked.iterator(); i.hasNext(); ) {
Entity clickable = i.next();
switch(clickable.onClicked(mode)) {
case ClickAction.IGNORE:
maybeSelect = true;
continue;
case ClickAction.DRAG:
// Allow dragging over factories & turrets. Unless in build mode.
if (mode == Mode.MODE_BUILD) {
waitForMouse = true;
leftMouseWasDown = true;
} else {
mode = Mode.MODE_DRAG;
leftMouseWasDown = false;
}
return;
case ClickAction.CONSUME:
if (mode == Mode.MODE_SELL) {
// Allow drag
leftMouseWasDown = false;
waitForMouse = false;
} else {
waitForMouse = true;
leftMouseWasDown = true;
}
return;
case ClickAction.FIRE:
if (mode == Mode.MODE_NORMAL) {
fire = true;
} else {
// Build or drag mode; don't zap
}
break;
}
}
if (!fire && mode == Mode.MODE_NORMAL) {
if (maybeSelect) {
mode = Mode.MODE_SCROLL;
Worm.setMouseAppearance(Res.getMousePointerGrab());
scrollOriginX = mouseX;
scrollOriginY = mouseY;
}
return;
}
} else {
if (mode == Mode.MODE_DRAG) {
Worm.setMouseAppearance(Res.getMousePointer());
}
}
if (mode == Mode.MODE_DRAG || mode == Mode.MODE_SELL || mode == Mode.MODE_SCROLL || mode == Mode.MODE_SELECT) {
return;
}
if (mode == Mode.MODE_BUILD) {
build();
break;
}
if (!capacitorRange) {
leftMouseWasDown = true;
Worm.setMouseAppearance(Res.getMousePointer());
if (mode == Mode.MODE_NORMAL) {
// Go into scroll mode
mode = Mode.MODE_SCROLL;
scrollOriginX = mouseX;
scrollOriginY = mouseY;
leftMouseWasDown = true;
Worm.setMouseAppearance(Res.getMousePointerGrab());
}
return;
} else {
leftMouseWasDown = true;
mode = Mode.MODE_ZAP;
armedCapacitors.clear();
armedCapacitors.addAll(TEMP_CAPACITORS);
}
case Mode.MODE_ZAP:
// fire. Otherwise do nothing.
for (int i = 0; i < armedCapacitors.size(); i++) {
Capacitor capacitor = armedCapacitors.get(i);
if (capacitor.isAlive()) {
capacitor.zap(x, y);
}
}
if (armedCapacitors.size() == 0) {
mode = Mode.MODE_NORMAL;
}
if (clicked.size() > 0) {
Worm.setMouseAppearance(clicked.get(0).getMousePointer(true));
} else if (isBezerk()) {
Worm.setMouseAppearance(capacitorRange ? Res.getMousePointerBezerkOffTarget() : Res.getMousePointerBezerkOutOfRange());
} else {
Worm.setMouseAppearance(capacitorRange ? Res.getMousePointerOffTarget() : Res.getMousePointerOutOfRange());
}
break;
case Mode.MODE_SMARTBOMB:
// Boooooooooom!
Smartbomb bomb = new Smartbomb(x, y);
bomb.spawn(GameScreen.getInstance());
mode = Mode.MODE_NORMAL;
// That'll probably change immediately next frame
Worm.setMouseAppearance(Res.getMousePointerOffTarget());
waitForMouse = true;
if (metaState.addStat(Stats.SMARTBOMBS_USED, 1) == 5) {
awardMedal(Medals.THE_ONLY_WAY_TO_BE_SURE);
}
break;
case Mode.MODE_SCROLL:
// Scroll the screen around under the mouse
int dx = scrollOriginX - mouseX;
int dy = scrollOriginY - mouseY;
scrollOriginX = mouseX;
scrollOriginY = mouseY;
GameScreen.getInstance().scroll(dx * MOUSE_DRAG_SPEED, dy * MOUSE_DRAG_SPEED);
break;
case Mode.MODE_SELECT:
// Do nothing
return;
default:
assert false;
}
} else if (auxDown) {
// Sell!
GameScreen.onMapGrabbedMouse(true);
for (Iterator<Entity> i = clicked.iterator(); i.hasNext(); ) {
Entity entity = i.next();
if (entity instanceof Building) {
Building building = (Building) entity;
if (building.canSell()) {
sell(building);
}
}
}
} else {
GameScreen.onMapGrabbedMouse(false);
boolean rmbDown = Mouse.getButtonCount() > 1 && Mouse.isButtonDown(1) || Game.wasKeyPressed(Keyboard.KEY_ESCAPE);
if (rmbDown) {
if (waitForMouse) {
return;
}
if (!rightMouseWasDown) {
scrollOriginX = mouseX;
scrollOriginY = mouseY;
rmbDragSensor = 0;
}
leftMouseWasDown = false;
rightMouseWasDown = true;
int dx = scrollOriginX - mouseX;
int dy = scrollOriginY - mouseY;
scrollOriginX = mouseX;
scrollOriginY = mouseY;
if (dx != 0 || dy != 0) {
rmbDragSensor++;
if (rmbDragSensor > RMB_DRAG_SENSITIVITY) {
GameScreen.getInstance().scroll(dx * MOUSE_DRAG_SPEED, dy * MOUSE_DRAG_SPEED);
rmbScroll = true;
Worm.setMouseAppearance(Res.getMousePointerGrab());
}
}
} else {
if (rightMouseWasDown && !rmbScroll) {
// It was a RMB click. Cancel building / selling / blowing stuff up. Or pick up whatever building is under the mouse to build.
switch(mode) {
case Mode.MODE_BUILD:
setBuilding(null);
rightMouseWasDown = false;
return;
case Mode.MODE_SELL:
setSellMode(false);
rightMouseWasDown = false;
return;
case Mode.MODE_DRAG:
case Mode.MODE_NORMAL:
case Mode.MODE_SELECT:
case Mode.MODE_ZAP:
for (Iterator<Entity> i = clicked.iterator(); i.hasNext(); ) {
Entity clickable = i.next();
if (clickable instanceof Building) {
BuildingFeature bf = ((Building) clickable).getFeature();
if (bf.isAvailable()) {
clickable.onLeave(mode);
lastHovered = null;
setBuilding(bf);
rightMouseWasDown = false;
return;
}
}
}
break;
case Mode.MODE_SMARTBOMB:
// Cancel the smartbomb and return it to the stores
addPowerup(SmartbombPowerupFeature.getInstance(), false);
mode = Mode.MODE_NORMAL;
rightMouseWasDown = false;
return;
case Mode.MODE_SCROLL:
break;
default:
assert false;
}
}
rmbScroll = false;
rightMouseWasDown = false;
leftMouseWasDown = false;
waitForMouse = false;
if (mode == Mode.MODE_BUILD) {
if (canBuild(false)) {
Worm.setMouseAppearance(Res.getMousePointer());
} else {
Worm.setMouseAppearance(Res.getMousePointerCantBuild());
}
} else if (hovered != null) {
Worm.setMouseAppearance(hovered.getMousePointer(false));
if (lastHovered != hovered) {
if (lastHovered != null) {
lastHovered.onLeave(mode);
}
lastHovered = hovered;
hovered.onHovered(mode);
}
} else {
if (lastHovered != null) {
lastHovered.onLeave(mode);
lastHovered = null;
}
if (mode == Mode.MODE_SMARTBOMB) {
Worm.setMouseAppearance(Res.getMousePointerSmartbomb());
} else if (mode == Mode.MODE_SELL) {
Worm.setMouseAppearance(Res.getMousePointerSellOff());
} else if (isBezerk()) {
Worm.setMouseAppearance(capacitorRange ? Res.getMousePointerBezerkOffTarget() : Res.getMousePointerBezerkOutOfRange());
} else {
Worm.setMouseAppearance(capacitorRange ? Res.getMousePointerOffTarget() : Res.getMousePointerOutOfRange());
}
}
if (mode == Mode.MODE_ZAP || mode == Mode.MODE_DRAG || mode == Mode.MODE_SCROLL || mode == Mode.MODE_SELECT) {
mode = Mode.MODE_NORMAL;
}
}
}
}