forked from mcyapan/CSCI205Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSceneCanvas.java
More file actions
144 lines (112 loc) · 3.47 KB
/
SceneCanvas.java
File metadata and controls
144 lines (112 loc) · 3.47 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
import java.awt.*;
import javax.swing.*;
public class SceneCanvas extends JComponent implements Runnable {
// PIXEL SETTINGS
private final int originalTileSize = 16;
private final int scale = 3;
// SCREEN SETTINGS
private final int tileSize = originalTileSize * scale;
private final int maxScreenCol = 18;
private final int maxScreenRow = 14;
private final int screenWidth = tileSize * maxScreenCol;
private final int screenHeight = tileSize * maxScreenRow;
// WORLD SETTINGS
private final int maxWorldCol = 50;
private final int maxWorldRow = 50;
// private final int worldWidth = tileSize * maxWorldCol;
// private final int worldHeight = tileSize * maxWorldRow;
// FPS SETTINGS
private int FPS = 60;
private TileManager tileManager = new TileManager(this);
private KeyHandle keyH = new KeyHandle();
private Thread gameThread;
private CollisionChecker cChecker = new CollisionChecker(this);
private AssetSetter aSetter = new AssetSetter(this);
private UI ui = new UI(this);
private Player player = new Player(this, keyH);
private DrawingObject[] obj = new DrawingObject[10];
public SceneCanvas() {
this.setPreferredSize(new Dimension(800, 600));
this.setBackground(Color.BLACK);
this.setDoubleBuffered(true);
this.addKeyListener(keyH);
this.setFocusable(true);
// need to populate arrays here
//trees[0] = new Tree(this, 100, 150); // First tree
//trees[1] = new Tree(this, 200, 250); // Second tree
//trees[2] = new Tree(this, 300, 100); // Third tree
}
public void setUpGame() {
aSetter.setObject();
}
public void startGameThread() {
gameThread = new Thread(this);
gameThread.start();
}
@Override
public void run() {
double drawInterval = 1000000000 / FPS;
double delta = 0;
long lastTime = System.nanoTime();
long currentTime;
while (gameThread != null) {
currentTime = System.nanoTime();
delta += (currentTime - lastTime) / drawInterval;
lastTime = currentTime;
if (delta >= 1) {
update();
repaint();
// System.out.println("FPS: 60");
delta--;
}
}
}
public void update() {
player.update();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
// Draw the tiles
tileManager.draw(g2d);
// Draw the object
for(int i = 0; i < obj.length; i++) {
if (obj[i] != null) {
obj[i].draw(g2d, this);
}
}
// Draw the player
player.draw(g2d);
// Draw UI
ui.draw(g2d);
g2d.dispose();
}
public int getTileSize() {
return tileSize;
}
public int getScreenWidth() {
return screenWidth;
}
public int getScreenHeight() {
return screenHeight;
}
public int getMaxWorldCol() {
return maxWorldCol;
}
public int getMaxWorldRow() {
return maxWorldRow;
}
public TileManager getTileManager() {
return tileManager;
}
public CollisionChecker getCChecker() {
return cChecker;
}
public Player getPlayer() {
return player;
}
public DrawingObject[] getObj() {
return obj;
}
}