-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpider.java
More file actions
136 lines (125 loc) · 3.83 KB
/
Copy pathSpider.java
File metadata and controls
136 lines (125 loc) · 3.83 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
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
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.*;
public class Spider {
private int x;
private int y;
private int limitTop;
private int limitBottom;
private int limitRight;
private int limitLeft;
private String direction = "up";
private BufferedImage spiderImage;
public Spider(int x, int y, String direction){
this.x = x;
this.y = y;
this.direction = direction;
try {
spiderImage = ImageIO.read(new File("spider_north.png"));
} catch (IOException e) {
e.printStackTrace();
}
Cell c1 = DataSource.getInstance().getCellArrayInstance().get(0);
limitTop = c1.getY();
limitLeft = c1.getX();
int size = DataSource.getInstance().getCellArrayInstance().size();
limitBottom = c1.getY() + (int)Math.sqrt(size)*35;
limitRight = c1.getX() + (int)Math.sqrt(size)*35;
System.out.println(c1.getX());
System.out.println(size);
}
public Boolean move(){
Boolean b = false;
switch(this.direction){
case "up":
this.y -= 35;
b = limitSpider();
break;
case "down":
this.y += 35;
b = limitSpider();
break;
case "left":
this.x -= 35;
b = limitSpider();
break;
case "right":
this.x += 35;
b = limitSpider();
break;
}
DataSource.getInstance().setSpiderLocation(this.x, this.y);
System.out.println("Spider X: " + this.x + ", Spider Y: " + this.y);
return b;
}
public void turn(){
switch(this.direction){
case "up":
this.direction = "right";
rotateImage(90);
break;
case "down":
this.direction = "left";
rotateImage(90);
break;
case "left":
this.direction = "up";
rotateImage(90);
break;
case "right":
this.direction = "down";
rotateImage(90);
break;
}
}
public void rotateImage(double angle) {
double rotationRequired = Math.toRadians(angle);
double locationX = spiderImage.getWidth() / 2;
double locationY = spiderImage.getHeight() / 2;
AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
spiderImage = op.filter(spiderImage, null);
}
public void draw(Graphics g){
if (spiderImage != null) {
g.drawImage(spiderImage, x-5, y+5, 25, 25, null);
}
}
public void setspiderX(int x){
this.x = x;
}
public void setSpiderY(int y){
this.y = y;
}
public int getSpiderX(){return this.x;}
public int getSpiderY(){return this.y;}
public void setDirection(String s){
this.direction = s;
}
public String getDirection(){
return this.direction;
}
public Boolean limitSpider(){
if(this.x >= this.limitRight){
this.x = this.limitRight - 25;
return true;
}
if(this.x <= this.limitLeft){
this.x = this.limitLeft + 10;
return true;
}
if(this.y >= this.limitBottom){
this.y = this.limitBottom - 35;
return true;
}
if(this.y < this.limitTop){
this.y = this.limitTop;
return true;
}
return false;
}
}