-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntity.java
215 lines (211 loc) · 6.31 KB
/
Entity.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//package
package entity;
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Rectangle;
//imports
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import Main.GamePanel;
import Main.UtilityTool;
//public class Entity
//Stores variables that will be used in player,monster and NPC classes.
public class Entity {
//GamePanel gp
GamePanel gp;
//public int x,y declared
public int worldX,worldY;
//public int speed declared
public int speed;
//boolean attacking=false;
boolean attacking = false;
//To store image file
public BufferedImage up1, up2, down1, down2, left1, left2, right1, right2;
public BufferedImage attackUp1,attackUp2,attackDown1,attackDown2,attackLeft1,attackLeft2,
attackRight1,attackRight2;
//Declare String direction
public String direction;
//Declare int spriteCounter as 0
public int spriteCounter = 0;
//Declare int spriteNum as 1
public int spriteNum = 1;
//public Boolean collisionOn is false
public boolean collisionOn=false;
//public int type
public int type;//1==monster
//public int actionLockCounter equal to zero
public int actionLockCounter=0;
//Public boolean invincible equals false
public boolean invincible=false;
//Public int invinciblecounter equals zero
public int invinciblecounter=0;
//Character Status
public int maxLife;
public int life;
//public Rectangle solidArea
public Rectangle solidArea = new Rectangle(0,0,48,48);
//public Rectangle attackArea
public Rectangle attackArea = new Rectangle(0,0,48,48);
//public int solidAreaDefaultX and solidAreaDefaultY
public int solidAreaDefaultX, solidAreaDefaultY;
//public Entity
public Entity(GamePanel gp) {
//this.gp=gp
this.gp=gp;
}//end public entity
//BufferedImage setup method
public BufferedImage setup(String imagePath,int width, int height) {
//instantiate utilityTool
UtilityTool uTool = new UtilityTool();
//image is equal to null
BufferedImage image = null;
//try
try {
//scale the player images
image = ImageIO.read(getClass().getResourceAsStream(imagePath+".png"));
image = uTool.scaleImage(image, width,height);
}//end try
//catch
catch(IOException e) {
e.printStackTrace();
}//end catch
//return image
return image;
}//end BufferedImage setup method
//public void draw
public void draw(Graphics2D g2) {
//BufferedImage is null
BufferedImage image = null;
int screenX = worldX-gp.player.worldX+gp.player.screenX;
int screenY = worldY-gp.player.worldY+gp.player.screenY;
if(worldX+gp.tileSize>gp.player.worldX-gp.player.screenX&&
worldX-gp.tileSize<gp.player.worldX+gp.player.screenX&&
worldY+gp.tileSize>gp.player.worldY-gp.player.screenY&&
worldY-gp.tileSize<gp.player.worldY+gp.player.screenY) {
//switch direction
switch(direction) {
//each case will update the image accordingly to create a working animation
case "up":
//if spriteNum equal 1
if(spriteNum==1) {
image = up1;
}//end if
//if spriteNum equal 2
if(spriteNum == 2) {
image = up2;
}//end if
break;
case "down":
//if spriteNum equal 1
if(spriteNum==1) {
image = down1;
}//end if
//if spriteNum equal 2
if(spriteNum == 2) {
image = down2;
}//end if
break;
case "left":
//if spriteNum equal to 1
if(spriteNum==1) {
image = left1;
}//end if
//if spriteNum equal to 2
if(spriteNum==2) {
image = left2;
}//end if
break;
case "right":
//if spriteNum equal to 1
if(spriteNum==1) {
image = right1;
}//end if
//if spriteNum equal to 2
if(spriteNum==2) {
image = right2;
}//end if
break;
}//end switch
//if invincible equals to true
if(invincible==true) {
//g2.setComposite for transparency
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f));
}//end if
//draw
g2.drawImage(image, screenX, screenY,gp.tileSize,gp.tileSize,null);
//g2.setComposite for transparency reset
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
}//end if
}//end draw
//public void setAction
public void setAction() {}
//public void update
public void update() {
//setAction method is called
setAction();
//collisionOn equal false
collisionOn=false;
gp.cChecker.checkTile(this);
boolean contactPlayer=gp.cChecker.checkPlayer(this);
//if the monster and contactPlayer is true
if(this.type==1&&contactPlayer==true) {
if(gp.player.invincible==false) {
//we can give damage
gp.player.life-=1;
gp.player.invincible=true;
//if gp.player.life is less than or equal to zero
if(gp.player.life<=0) {
//gp.player is equal to null
gp.player=null;
}//end if
}//end if
}//end if
//if collision on is false
if(collisionOn == false) {
//switch base off of direction
switch(direction) {
case "up":worldY -=speed;
break;
case "down":worldY +=speed;
break;
case "left":worldX-=speed;
break;
case "right":worldX +=speed;
break;
}//end switch
}//end if
//In every frame the update method gets called, increases the counter by 1
//increment the spriteCounter
spriteCounter++;
//if it reaches 12 it changes the sprite, since the update method is 60 times per
//second then the image changes every 12 frames.
//if spriteCounter greater than 12
if(spriteCounter>12) {
//if spriteNum equal to 1
if(spriteNum==1) {
//then spriteNum is 2
spriteNum=2;
}//end if
//else if spriteNum equal to 2
else if(spriteNum==2) {
//then spriteNum is equal to 1
spriteNum=1;
}//end else if
//the spriteCounter is reset
spriteCounter=0;
}//end if
//if invincible==true
if(invincible==true) {
//invinciblecounter is being incremented
invinciblecounter++;
//if invinciblecounter is greater than 40
if(invinciblecounter>40) {
//invincible equals false
invincible=false;
//invinciblecounter equals to zero
invinciblecounter=0;
}//end if
}//end if
}//end update
}//end class