/
Enemy.java
90 lines (73 loc) · 2.03 KB
/
Enemy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.lud.root.jetfighter;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import java.util.Random;
public class Enemy {
private int x;
private int y;
private int speed = -1;
private int radius = 20;
private int maxX, minX;
private int maxY, minY;
//Create a rect object to detect collision
private Rect detectCollision;
public Enemy(Context context, int screenX, int screenY){
maxX = screenX;
maxY = screenY;
minX = 0;
minY = 0;
// Randomly generating enemy position
Random generator = new Random();
speed = 25;
radius = 35;
x = screenX;
y = generator.nextInt(maxY) + 10 - radius;
}
public Enemy(Context context, int screenX, int screenY, int prevY, int prevRad){
maxX = screenX;
maxY = screenY;
minX = 0;
minY = 0;
// Randomly generating enemy position
Random generator = new Random();
speed = 25;
radius = 35;
x = screenX;
do{
y = generator.nextInt(maxY) + 10 - radius;
}while ((y - prevY) < (prevRad + radius + 60));
}
public void update(int playerSpeed){
// As the enemy moves from right to left
x -= playerSpeed;
x -= speed;
Random generator = new Random();
if(x < minX - this.getRadius()){
speed = generator.nextInt(10) + 10;
radius = generator.nextInt(20) + 30;
x = maxX;
y = generator.nextInt(maxY) - radius;
}
}
//This setter is used for changing the x coordinate after collision
public void setX(int x) {
this.x = x;
}
public Rect getDetectCollision() {
return detectCollision;
}
public int getSpeed() {
return speed;
}
public int getY() {
return y;
}
public int getX() {
return x;
}
public int getRadius() {
return radius;
}
}