Allow enemies move by difficult mode
This commit is contained in:
parent
922871be0c
commit
8ff66abc0a
@ -15,6 +15,7 @@ public class EnemiesController {
|
||||
|
||||
public EnemiesController() {
|
||||
enemies = new Array<Enemy>();
|
||||
difficultMode = 0;
|
||||
}
|
||||
|
||||
public void setScreenParameters(float width, float height) {
|
||||
@ -35,10 +36,12 @@ public class EnemiesController {
|
||||
}
|
||||
|
||||
public boolean isCollide(float x, float y) {
|
||||
boolean collide = false;
|
||||
for (Enemy enemy : enemies) {
|
||||
if (enemy.isCollide(x, y)) return true;
|
||||
enemy.update(x, y, screenWidth, screenHeight, difficultMode);
|
||||
if (enemy.isCollide(x, y)) collide = true;
|
||||
}
|
||||
return false;
|
||||
return collide;
|
||||
}
|
||||
|
||||
public void killEnemy(float x, float y) {
|
||||
|
@ -2,6 +2,7 @@ package com.annimon.influencexxii;
|
||||
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
||||
import com.badlogic.gdx.math.MathUtils;
|
||||
import com.badlogic.gdx.math.Rectangle;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
|
||||
@ -33,4 +34,51 @@ public class Enemy {
|
||||
renderer.setColor(color);
|
||||
renderer.rect(rect.x, rect.y, rect.width, rect.height);
|
||||
}
|
||||
|
||||
public void update(float px, float py, float scrWidth, float scrHeight, int difficultMode) {
|
||||
if (difficultMode == 0) return;
|
||||
|
||||
final int speed = 1 + difficultMode / 3;
|
||||
changeCoords(speed);
|
||||
boolean changeDir = validateToScreen(scrWidth, scrHeight);
|
||||
|
||||
switch (difficultMode % 3) {
|
||||
case 2: // Medium
|
||||
if (rect.contains(px, py)) {
|
||||
rect.x += dir.x * speed;
|
||||
}
|
||||
break;
|
||||
case 0: // Difficult
|
||||
if ( (rect.contains(px - 3, py - 3)) || (rect.contains(px + 3, py + 3)) ) {
|
||||
if (Math.abs(dir.x) < 0.2f) dir.x = MathUtils.random(-1f, 1f);
|
||||
if (Math.abs(dir.y) < 0.2f) dir.y = MathUtils.random(-1f, 1f);
|
||||
changeCoords(speed);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (changeDir) inverseDirection();
|
||||
else if (MathUtils.random(50) == 10) {
|
||||
changeDirection();
|
||||
}
|
||||
}
|
||||
|
||||
private void changeCoords(int speed) {
|
||||
rect.x += dir.x * speed;
|
||||
rect.y += dir.y * speed;
|
||||
}
|
||||
|
||||
private void changeDirection() {
|
||||
dir.x = MathUtils.random(-1f, 1f);
|
||||
dir.y = MathUtils.random(-1f, 1f);
|
||||
}
|
||||
|
||||
private void inverseDirection() {
|
||||
dir.scl(-1f, -1f);
|
||||
}
|
||||
|
||||
private boolean validateToScreen(float width, float height) {
|
||||
Rectangle screen = new Rectangle(0, 0, width, height);
|
||||
return (!screen.contains(rect));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user