This repository has been archived by the owner on Mar 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathItem.java
147 lines (131 loc) · 3.61 KB
/
Item.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;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
public class Item{
//handler
public static Item[] items = new Item[100];
public static Item woodenKey = new Item(Assets.woodenKey,"Wooden Key",0);
public static Item metalKey = new Item(Assets.metalKey,"Metallic Key",1);
public static Item boneKey = new Item(Assets.boneKey,"Bone Key",2);
public static Item fishKey = new Item(Assets.fishKey,"Fish Key",3);
public static Item scoreCoin = new Item(Assets.scoreCoin,"Score Coin",4);
public static Item demoKey = new Item(Assets.fishKey,"Demo Key",5);
//class
public static final int ITEMWIDTH = 50, ITEMHEIGHT = 50;
protected boolean pickedUp = false;
protected Handler handler;
protected BufferedImage texture;
protected BufferedImage[] texture2;
protected Animation animTexture;
protected String name;
protected final int id;
protected boolean animatedItem;
protected int x,y, count;
protected Rectangle bounds;
public Item(BufferedImage texture, String name, int id){
this.texture = texture;
this.name = name;
this.id= id;
count = 1;
animatedItem = false;
bounds = new Rectangle(x, y, ITEMWIDTH, ITEMHEIGHT);
items[id] = this;
}
public Item(BufferedImage[] texture2, String name, int id){
this.texture2 = texture2;
animTexture = new Animation(100, texture2);
this.name = name;
this.id= id;
count = 1;
animatedItem = true;
bounds = new Rectangle(x, y, ITEMWIDTH, ITEMHEIGHT);
items[id] = this;
}
public void update(){
if(animatedItem)
animTexture.update();
if(handler.getLevel().getEntityManager().getPlayer().getCollisionBounds(0f,0f).intersects(bounds)){
pickedUp = true;
if(id == 5){
handler.setNotification("Open door using W(up),S(down),A(left),D(right)", 5);
}
handler.getLevel().getEntityManager().getPlayer().getInventory().addItem(this);
handler.getLevel().getEntityManager().getPlayer().getInventory().checkLevelComplete();
}
}
public void render(Graphics g){
if(handler == null){
return;
}
render(g,(int)(x - handler.getCamera().getxOffset()),(int)(y - handler.getCamera().getyOffset()));
}
public void render(Graphics g, int x, int y){
g.drawImage(getTexture(),x,y,ITEMWIDTH,ITEMHEIGHT,null);
}
public void setPosition(int x,int y){
this.x = x;
this.y = y;
bounds.x = x;
bounds.y = y;
}
public Item createNew(int x, int y){
Item i;
if(animatedItem){
i = new Item(texture2, name, id);
}else{
i = new Item(texture, name, id);
}
i.setPosition(x,y);
return i;
}
//getters and setters
public void setCount(int count){
this.count = count;
}
public void setX(int x){
this.x = x;
}
public void setY(int y){
this.y = y;
}
public void setHandler(Handler handler){
this.handler= handler;
}
public void setTexture(BufferedImage texture){
this.texture= texture;
}
public void setTexture(BufferedImage[] texture){
animTexture = new Animation(250,texture);
}
public void setName(String name){
this.name= name;
}
public BufferedImage getTexture(){
if(animatedItem){
return animTexture.getCurrentFrame();
}else {
return texture;
}
}
public int getCount(){
return count;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public Handler getHandler(){
return handler;
}
public int getId(){
return id;
}
public String getName(){
return name;
}
public boolean isPickedUp(){
return pickedUp;
}
}