-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bumper.java
147 lines (147 loc) · 4.14 KB
/
Bumper.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import java.awt.Rectangle;
public class Bumper{
double x,y,width,height;
boolean passthrough = false;
Rectangle bounds;
public Bumper(double x,double y,double width, double height){
this(x,y,width,height,false);
}
public Bumper(double x,double y,double width,double height,boolean pt){
this.x=x;
this.y=y;
this.width=width;
this.height=height;
bounds = new Rectangle((int)x,(int)y,(int)width,(int)height);
passthrough = pt;
}
public double getX(){
return x;
}
public double getY(){
return y;
}
public double getWidth(){
return width;
}
public double getHeight(){
return height;
}
public void draw(java.awt.Graphics g){
if(this instanceof SpeedBumper)
{
g.setColor(java.awt.Color.green);
}
else
{
g.setColor(java.awt.Color.BLUE.darker().darker());//new java.awt.Color(133,57,0));
g.setColor(java.awt.Color.BLUE.darker().darker());//new java.awt.Color(133,57,0));
g.fillRect((int)x,(int)y,(int)width,(int)height);
}
}
public void draw(java.awt.Graphics g, double xScale, double yScale){
if(this instanceof SpeedBumper)
{
g.setColor(java.awt.Color.green);
}
else
{
g.setColor(java.awt.Color.BLUE.darker().darker());//new java.awt.Color(133,57,0));
}
g.fillRect((int)(x*xScale),(int)(y*yScale),(int)(width*xScale),(int)(height*yScale));
}
public void draw(java.awt.Graphics g, double xScale, double yScale,java.awt.Color c){
g.setColor(c);//new java.awt.Color(133,57,0));
g.fillRect((int)(x*xScale),(int)(y*yScale),(int)(width*xScale),(int)(height*yScale));
}
public boolean hIntersects(Item i){
return hIntersects(i.x,i.y,i.r);
}
public boolean hIntersects(double ix, double iy, double ir){
if(ix+ir>x && ix+ir<x+width && iy>y && iy<y+height)
return true;
else if(ix-ir>x && ix-ir<x+width && iy>y && iy<y+height)
return true;
else
return false;
}
public boolean vIntersects(Item i){
return vIntersects(i.x,i.y,i.r);
}
public boolean vIntersects(double ix, double iy, double ir){
if(ix>x && ix<width+x && iy+ir>y && iy+ir<y+height)
return true;
else if(ix>x && ix<x+width && iy-ir>y && iy-ir<y+height)
return true;
else
return false;
}
private boolean check2(double hx, double hy, double hradius){
return (vIntersects(hx,hy,hradius)||hIntersects(hx,hy,hradius)||cornerCheck(hx,hy,hradius));
}
public boolean check(Hero h){
return check(h.x,h.y,h.dx,h.dy,h.r);
}
public boolean check(double hx, double hy,double hdx, double hdy, double hr){
if (passthrough){
if(overBumper(hx+hdx,hy,hr)){
if (overBumper(hx+hdx,hy+hdy,hr)){
return false;
}
else{
return true;
}
}
else
return (onBumper(hx,hy,hr)&&(hdy>=0));
}
else
return check2(hx+hdx,hy+hdy,hr);
}
public boolean cornerCheck(Item i){
return cornerCheck(i.x,i.y,i.r);
}
public boolean cornerCheck(double ix, double iy, double ir){
if (distance(x,y,ix,iy)<ir)
return true;
else if (distance(x+width,y,ix,iy)<ir)
return true;
else if (distance(x,y+height,ix,iy)<ir)
return true;
else if (distance(x+width,y+height,ix,iy)<ir)
return true;
else
return false;
}
public boolean onBumper(double ix, double iy, double ir){
if ((iy+ir==y)&&(ix>=x)&&(ix<=x+width))
return true;
else if (distance(x,y,ix,iy)==ir)
return true;
else if (distance(x+width,y,ix,iy)==ir)
return true;
else
return false;
}
public boolean onBumper(Hero h){
return onBumper(h.x,h.y,h.r);
}
public boolean overBumper(double ix, double iy, double ir){
if ((iy+ir<y)&&(ix>=x)&&(ix<=x+width))
return true;
else if ((distance(x,y,ix,iy)>ir)&&(iy<y)&&(ix>=x-ir)&&(ix<=x))
return true;
else if ((distance(x+width,y,ix,iy)>ir)&&(iy<y)&&(ix<=x+width+ir)&&(ix>=x+width))
return true;
else
return false;
}
public boolean overBumper(Item h){
return overBumper(h.x,h.y,h.r);
}
private static double distance(double x1, double y1, double x2, double y2){
return Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2));
}
private static double distance(double x1, double y1, Item i){
return distance(x1,y1,i.x,i.y);
}
}