-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChest.java
More file actions
155 lines (104 loc) · 2.88 KB
/
Copy pathChest.java
File metadata and controls
155 lines (104 loc) · 2.88 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.util.Random;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Image;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
public class Chest{
private static final int XSIZE = 70; // width of the image
private static final int YSIZE = 50; // height of the image
//private static final int DX = 2; // amount of pixels to move in one update
private static final int YPOS = 150; // vertical position of the image
private JFrame panel; // JPanel on which image will be drawn
private Dimension dimension;
private int x;
private int y;
private int dx;
private Player player;
private Image spriteImage; // image for sprite
private Image chestClose;
private Image chestOpening;
//Graphics2D g2;
int time, timeChange; // to control when the image is grayed
boolean originalImage, grayImage;
public Chest (JFrame panel, Player player, int x, int y) {
this.panel = panel;
//Graphics g = window.getGraphics ();
//g2 = (Graphics2D) g;
dimension = panel.getSize();
Random random = new Random();
setPos(x, y);
dx = 0;
this.player = player;
time = 0; // range is 0 to 10
timeChange = 1; // set to 1
originalImage = true;
grayImage = false;
chestClose= ImageManager.loadImage("images/chestIdle.png");
chestOpening= ImageManager.loadImage("images/chestOpen.gif");
spriteImage = chestClose;
}
public void setPos(int x, int y){
this.x= x;
this.y = y;
}
public void update(){
if(isNearPlayer()){
spriteImage = chestOpening;
}
}
public void draw (Graphics2D g2) {
g2.drawImage(spriteImage, x, y, XSIZE, YSIZE, null);
}
public boolean collidesWithPlayer () {
Rectangle2D.Double myRect = getBoundingRectangle();
Rectangle2D.Double playerRect = player.getBoundingRectangle();
if (myRect.intersects(playerRect)) {
System.out.println ("Collision with player!");
return true;
}
else
return false;
}
public boolean isNearPlayer(){
if(Math.abs(this.x - player.getX()) <= 400)
return true;
else
return false;
}
public Rectangle2D.Double getBoundingRectangle() {
return new Rectangle2D.Double (x, y, XSIZE-15, YSIZE-15);
}
// public void update() {
// x = x + dx;
// if (x < 4064 || x > 4184)
// dx = dx * -1;
// }
public int getX() {
return x;
}
public int getWidth(){
return this.XSIZE;
}
public int getHeight(){
return this.YSIZE;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Image getImage() {
return spriteImage;
}
}