Skip to content

Commit d9ff1c6

Browse files
Initial commit
0 parents  commit d9ff1c6

7 files changed

Lines changed: 781 additions & 0 deletions

File tree

src/CoolPane.java

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import java.util.List;
2+
3+
import javax.swing.*;
4+
import java.awt.*;
5+
import java.awt.event.MouseAdapter;
6+
import java.awt.event.MouseEvent;
7+
import java.awt.event.MouseWheelEvent;
8+
import java.awt.image.BufferedImage;
9+
10+
public class CoolPane extends JPanel {
11+
12+
13+
private float dx, dy, zoom;
14+
15+
private int last_x, last_y;
16+
17+
private JTextField in;
18+
19+
/*
20+
I dont like boxlayout, maybe i will change this someday
21+
*/
22+
public CoolPane(JTextField in) {
23+
24+
this.in = in;
25+
26+
centerCamera();
27+
28+
this.addMouseWheelListener(new MouseAdapter() {
29+
@Override
30+
public void mouseWheelMoved(MouseWheelEvent e) {
31+
zoom *= Math.pow(1.2, -e.getPreciseWheelRotation());
32+
}
33+
});
34+
35+
this.addMouseMotionListener(new MouseAdapter() {
36+
37+
@Override
38+
public void mouseMoved(MouseEvent e) {
39+
last_x = e.getX();
40+
last_y = e.getY();
41+
}
42+
43+
@Override
44+
public void mouseDragged(MouseEvent e) {
45+
if (SwingUtilities.isMiddleMouseButton(e)) {
46+
dx += (e.getX() - last_x) / zoom;
47+
dy += (e.getY() - last_y) / zoom;
48+
}
49+
50+
last_x = e.getX();
51+
last_y = e.getY();
52+
}
53+
});
54+
55+
this.addMouseListener(new MouseAdapter() {
56+
57+
@Override
58+
public void mousePressed(MouseEvent e) {
59+
if(e.getButton() != 2) centerCamera();
60+
}
61+
});
62+
}
63+
64+
private void centerCamera() {
65+
String size = in.getText();
66+
if(size != "")
67+
this.dx = -Integer.parseInt(size) / 2;
68+
this.dy = -Integer.parseInt(size) / 2;
69+
this.zoom = 0.5f;
70+
}
71+
private Location getBlockLocation(int xPos, int yPos) {
72+
float x = xPos, y = yPos;
73+
x -= this.getWidth() / 2.0;
74+
y -= this.getHeight() / 2.0;
75+
x /= zoom;
76+
y /= zoom;
77+
x -= dx;
78+
y -= dy;
79+
x = (float) Math.floor(x);
80+
y = (float) Math.floor(y);
81+
82+
return new Location(x, y);
83+
}
84+
85+
public void paintComponent(List<Entity> images, int size, boolean wire) {
86+
Graphics g = this.getGraphics();
87+
BufferedImage img = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_ARGB);
88+
Graphics2D g2 = (Graphics2D) img.getGraphics();
89+
90+
g2.setColor(new Color(240, 248, 255));
91+
g2.fillRect(0, 0, img.getWidth(), img.getHeight());
92+
93+
g2.translate(this.getWidth() / 2.0, this.getHeight() / 2.0);
94+
g2.scale(zoom, zoom);
95+
g2.translate(dx, dy);
96+
97+
g2.setColor(Color.LIGHT_GRAY);
98+
g2.fillRect(0, 0, (size), (size));
99+
100+
g2.setStroke(new BasicStroke(0.1f));
101+
g2.setColor(Color.RED);
102+
for(int i = 0; i < images.size(); i++) {
103+
Entity e = images.get(i);
104+
if(wire) g2.drawRect((int)e.getPosition().x, (int)e.getPosition().y, (int)e.getWidth(), (int)e.getHeight());
105+
else g2.drawImage(TextureHandler.getImagePng(e.getName()), (int)e.getPosition().x, (int)e.getPosition().y, null);
106+
}
107+
108+
g.drawImage(img, 0, 0, null);
109+
}
110+
}

src/Entity.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import com.sun.javafx.geom.Vec2d;
2+
3+
public class Entity {
4+
5+
private String name;
6+
7+
private Location pos;
8+
private Vec2d velo;
9+
private double width, height;
10+
11+
public Entity(String name, double x, double y, double w, double h) {
12+
this.name = name;
13+
velo = new Vec2d(0, 0);
14+
pos = new Location(x, y);
15+
this.width = w;
16+
this.height = h;
17+
}
18+
19+
public void setPosition(int x, int y) {
20+
this.pos = new Location(x, y);
21+
}
22+
23+
@Override
24+
public boolean equals(Object n) {
25+
if(n instanceof Entity) {
26+
Entity e = (Entity) n;
27+
return pos.equals(e.pos) && velo.equals(e.velo) && width == e.width && height == e.height;
28+
}
29+
return false;
30+
}
31+
32+
public void move(int xb, int yb) {
33+
if(pos.x < 0) pos.x = 0;
34+
if(pos.x + width > xb) pos.x = xb - width;
35+
36+
if(pos.y < 0) pos.y = 0;
37+
if(pos.y + height > yb) pos.y = yb - height;
38+
pos.add(velo.x, velo.y);
39+
}
40+
41+
public void setVelocity(double x, double y) {
42+
velo = new Vec2d(x, y);
43+
}
44+
45+
public Vec2d getVelocity() {
46+
return velo;
47+
}
48+
49+
public Location getPosition() {
50+
return pos;
51+
}
52+
53+
public double getWidth() {
54+
return width;
55+
}
56+
57+
public double getHeight() {
58+
return height;
59+
}
60+
61+
public String getName() {
62+
return name;
63+
}
64+
}

src/Location.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
public class Location {
2+
public double x;
3+
public double y;
4+
5+
public Location(double x, double y) {
6+
this.x = x;
7+
this.y = y;
8+
}
9+
10+
public Location add(double xOff, double yOff) {
11+
x += xOff;
12+
y += yOff;
13+
14+
return this;
15+
}
16+
17+
public void divide(double xOff, double yOff) {
18+
x /= xOff;
19+
y /= yOff;
20+
}
21+
22+
public void multi(double xOff, double yOff) {
23+
x *= xOff;
24+
y *= yOff;
25+
}
26+
27+
public double length() {
28+
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
29+
}
30+
31+
public void normalize(int norm) {
32+
double l = length();
33+
if(l != 0) divide(l, l);
34+
else {
35+
x = 0;
36+
y = 0;
37+
}
38+
multi(norm, norm);
39+
}
40+
41+
@Override
42+
public int hashCode() {
43+
double hash = 17L;
44+
hash = hash*31 + x;
45+
hash = hash*31 + y;
46+
47+
Long l = new Double(hash).longValue();
48+
return l.intValue();
49+
}
50+
51+
@Override
52+
public Location clone() {
53+
return new Location(x, y);
54+
}
55+
56+
@Override
57+
public boolean equals(Object obj) {
58+
if (obj instanceof Location) {
59+
Location b = (Location) obj;
60+
return x == b.x && y == b.y;
61+
}
62+
return false;
63+
}
64+
65+
public double distanceTo(Location loc2) {
66+
return Math.sqrt(Math.pow(loc2.x - x, 2) + Math.pow(loc2.y - y, 2));
67+
}
68+
69+
@Override
70+
public String toString() {
71+
return String.format("(%f | %f)", x, y);
72+
}
73+
}

src/Main.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public class Main {
2+
3+
public static void main(String[] args) {
4+
5+
new Window();
6+
7+
}
8+
}

src/Steering.java

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import java.util.List;
2+
3+
public class Steering {
4+
5+
private List<Entity> entities;
6+
public Steering(List<Entity> entities) {
7+
this.entities = entities;
8+
}
9+
10+
public Location steer(Entity e) {
11+
12+
Location a = align(e, 10);
13+
Location s = separation(e, 2);
14+
Location c = cohesion(e, 10);
15+
16+
Location l = new Location(s.x + c.x + a.x, s.y + c.y + a.y);
17+
l.normalize(1);
18+
return l;
19+
}
20+
21+
public Location align(Entity e, double distance) {
22+
int neigth = 0;
23+
Location v = new Location(0, 0);
24+
25+
for(Entity en: entities) {
26+
if(!e.equals(en)) {
27+
if(insight(e, en, distance)) {
28+
v.add(en.getVelocity().x, en.getVelocity().y);
29+
neigth++;
30+
}
31+
}
32+
}
33+
34+
if(neigth == 0) return v;
35+
36+
v.divide(neigth, neigth);
37+
v.normalize(1);
38+
39+
return v;
40+
}
41+
42+
public Location cohesion(Entity e, double distance) {
43+
int neigth = 0;
44+
Location v = new Location(0, 0);
45+
46+
for(Entity en: entities) {
47+
if(!e.equals(en)) {
48+
if(insight(e, en, distance)) {
49+
v.add(en.getPosition().x, en.getPosition().y);
50+
neigth++;
51+
}
52+
}
53+
}
54+
55+
if(neigth == 0) return v;
56+
57+
v.divide(neigth, neigth);
58+
Location a = new Location(v.x - e.getPosition().x, v.y - e.getPosition().y);
59+
a.normalize(1);
60+
61+
return a;
62+
}
63+
64+
public Location separation(Entity e, double distance) {
65+
int neigth = 0;
66+
Location v = new Location(0, 0);
67+
68+
for(Entity en: entities) {
69+
if(!e.equals(en)) {
70+
if(insight(e, en, distance)) {
71+
v.add(en.getPosition().x - e.getPosition().x, en.getPosition().y - e.getPosition().y);
72+
neigth++;
73+
}
74+
}
75+
}
76+
77+
if(neigth == 0) return v;
78+
79+
v.divide(neigth, neigth);
80+
v.multi(-1, -1);
81+
v.normalize(1);
82+
83+
return v;
84+
}
85+
86+
private boolean inside(double x1, double y1, double width1, double height1, double x2, double y2, double width2, double height2) {
87+
return (x2 + width2 > x1 && y2 + height2 > y1 && x1 + width1 > x2 && y1 + height1 > y2);
88+
}
89+
90+
private boolean insight(Entity e1, Entity e2, double distance) {
91+
Location l = e1.getPosition();
92+
return insight_1(l, e2, distance) || insight_1(l.clone().add(e1.getWidth(), 0), e2, distance) || insight_1(l.clone().add(e1.getWidth(), e1.getHeight()), e2, distance) || insight_1(l.clone().add(0, e1.getHeight()), e2, distance) || inside(l.x, l.y, e1.getWidth(), e1.getHeight(), e2.getPosition().x, e2.getPosition().y, e2.getWidth(), e2.getHeight());
93+
}
94+
95+
private boolean insight_1(Location l1, Entity e, double distance) {
96+
Location l2 = e.getPosition();
97+
98+
return l2.distanceTo(l1) < distance || l2.clone().add(0, e.getHeight()).distanceTo(l1) < distance || l2.clone().add(e.getWidth(), e.getHeight()).distanceTo(l1) < distance || l2.clone().add(e.getWidth(), 0).distanceTo(l1) < distance;
99+
}
100+
}

0 commit comments

Comments
 (0)