Skip to content

Commit c60f293

Browse files
committed
Making a window for the inventory
Added a new Window for inventory when a player moves next to a chest. Modified other files for the window to work properly May have a few issues/bugs
1 parent 24408b4 commit c60f293

File tree

4 files changed

+106
-1
lines changed

4 files changed

+106
-1
lines changed

src/com/redomar/game/menu/DedicatedJFrame.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public DedicatedJFrame(int WIDTH, int HEIGHT, int SCALE, String NAME) {
1717
setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
1818

1919
setFrame(new JFrame(NAME));
20-
getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
20+
//getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
2121
getFrame().setLayout(new BorderLayout());
2222
getFrame().add(this, BorderLayout.CENTER);
2323
getFrame().pack();

src/com/redomar/game/menu/Menu.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.awt.event.MouseMotionListener;
88
import java.awt.image.BufferStrategy;
99

10+
import javax.swing.JFrame;
1011
import javax.swing.JOptionPane;
1112
import javax.swing.UIManager;
1213

@@ -226,6 +227,8 @@ public static void play() {
226227
Thread.sleep(250);
227228
splash.splashOff();
228229
frame = new DedicatedJFrame(WIDTH, HEIGHT, SCALE, NAME);
230+
frame.getFrame();
231+
frame.getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
229232
} catch (Exception e) {
230233
e.printStackTrace();
231234
}

src/com/redomar/game/objects/Inventory.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class Inventory {
77
public static int x;
88
public static int y;
99
public static boolean open = false;
10+
private static InventoryWindow inv_window = new InventoryWindow();
1011

1112
public static void activate() {
1213
x = Game.getPlayer().getX();
@@ -16,10 +17,15 @@ public static void activate() {
1617
if(!open){
1718
System.out.println("Opened\nInside this Bag their is:"+inside());
1819
open = true;
20+
Game.getPlayer().setMoving(false);
21+
inv_window.start();
1922
}
2023
}else{
2124
if(open){
2225
open = false;
26+
inv_window.stop();
27+
inv_window.getFrame().setVisible(false);
28+
inv_window.getFrame().stopFrame();
2329
}
2430
}
2531
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.redomar.game.objects;
2+
3+
import java.awt.Color;
4+
import java.awt.Graphics;
5+
import java.awt.image.BufferStrategy;
6+
7+
import com.redomar.game.menu.DedicatedJFrame;
8+
9+
public class InventoryWindow implements Runnable{
10+
11+
private static final int WIDTH = 160;
12+
private static final int HEIGHT = (WIDTH / 3 * 2);
13+
private static final int SCALE = 2;
14+
private static final String NAME = "Inventory";
15+
16+
private static boolean running = false;
17+
18+
private static DedicatedJFrame frame;
19+
20+
public synchronized void start(){
21+
running = true;
22+
setFrame(new DedicatedJFrame(WIDTH, HEIGHT, SCALE, NAME));
23+
new Thread(this, NAME).start();
24+
}
25+
26+
public synchronized void stop(){
27+
running = false;
28+
}
29+
30+
public void run() {
31+
long lastTime = System.nanoTime();
32+
double nsPerTick = 1000000000D / 30D;
33+
34+
int ticks = 0;
35+
int frames = 0;
36+
37+
long lastTimer = System.currentTimeMillis();
38+
double delta = 0;
39+
40+
while (running) {
41+
long now = System.nanoTime();
42+
delta += (now - lastTime) / nsPerTick;
43+
lastTime = now;
44+
boolean shouldRender = false;
45+
46+
while (delta >= 1) {
47+
ticks++;
48+
delta -= 1;
49+
shouldRender = true;
50+
}
51+
52+
try {
53+
Thread.sleep(2);
54+
} catch (InterruptedException e) {
55+
e.printStackTrace();
56+
}
57+
58+
if (shouldRender) {
59+
frames++;
60+
render();
61+
}
62+
63+
if (System.currentTimeMillis() - lastTimer >= 1000) {
64+
lastTimer += 1000;
65+
getFrame().getFrame().setTitle(
66+
"Frames: " + frames + " Ticks: " + ticks);
67+
frames = 0;
68+
ticks = 0;
69+
}
70+
}
71+
}
72+
73+
private void render() {
74+
BufferStrategy bs = getFrame().getBufferStrategy();
75+
if(bs == null){
76+
getFrame().createBufferStrategy(3);
77+
return;
78+
}
79+
80+
Graphics g = bs.getDrawGraphics();
81+
g.setColor(Color.BLACK);
82+
g.fillRect(0, 0, WIDTH*SCALE+10, HEIGHT*SCALE+10);
83+
g.setColor(Color.WHITE);
84+
g.drawString(NAME, 50, 50);
85+
bs.show();
86+
g.dispose();
87+
}
88+
89+
public DedicatedJFrame getFrame() {
90+
return frame;
91+
}
92+
93+
public static void setFrame(DedicatedJFrame frame) {
94+
InventoryWindow.frame = frame;
95+
}
96+
}

0 commit comments

Comments
 (0)