-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmartEnemy.java
More file actions
54 lines (38 loc) · 1.13 KB
/
SmartEnemy.java
File metadata and controls
54 lines (38 loc) · 1.13 KB
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
package com.tutorial.main;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
public class SmartEnemy extends GameObject
{
private Handler handler;
private GameObject player;
public SmartEnemy(int x, int y, ID id, Handler handler)
{
super(x, y, id);
this.handler = handler;
for(int i = 0; i < handler.object.size(); i++)
{
if(handler.object.get(i).getId() == ID.Player) player = handler.object.get(i);
}
}
public Rectangle getBounds()
{
return new Rectangle((int)x, (int)y, 16, 16);
}
public void tick()
{
x += velX;
y += velY;
float diffX = x - player.getX() - 8;
float diffY = y - player.getY() - 8;
float distance = (float)Math.sqrt((x-player.getX()) * (x - player.getX()) + (y-player.getY()) * (y-player.getY()));
velX = (float) ((-1.0/distance)*diffX);
velY = (float) ((-1.0/distance)*diffY);
handler.addObject(new Trail((int)x, (int)y, ID.Trail, Color.green, 16, 16, 0.015f, handler));
}
public void render(Graphics g)
{
g.setColor(Color.green);
g.fillRect((int)x, (int)y, 16, 16);
}
}