Skip to content

Commit 4f557c4

Browse files
committed
Added new class Scenes
Cleaned some varibles
1 parent 2642514 commit 4f557c4

File tree

2 files changed

+80
-28
lines changed

2 files changed

+80
-28
lines changed

src/com/redomar/game/level/LevelHandler.java

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.redomar.game.level.tiles.Tile;
2020
import com.redomar.game.lib.utils.Vector2i;
2121
import com.redomar.game.net.packets.Packet01Disconnect;
22+
import com.redomar.game.scenes.Scene;
2223
import com.redomar.game.script.PrintTypes;
2324
import com.redomar.game.script.Printing;
2425

@@ -49,9 +50,9 @@ public LevelHandler(String imagePath) {
4950
this.imagePath = imagePath;
5051
this.loadLevelFromFile();
5152
} else {
52-
tiles = new byte[width * height];
53-
this.width = 64;
54-
this.height = 64;
53+
tiles = new byte[getWidth() * getHeight()];
54+
this.setWidth(64);
55+
this.setHeight(64);
5556
this.generateLevel();
5657
}
5758

@@ -61,24 +62,24 @@ public LevelHandler(String imagePath) {
6162
private void loadLevelFromFile() {
6263
try {
6364
this.image = ImageIO.read(Level.class.getResource(this.imagePath));
64-
this.width = image.getWidth();
65-
this.height = image.getHeight();
66-
tiles = new byte[width * height];
65+
this.setWidth(image.getWidth());
66+
this.setHeight(image.getHeight());
67+
tiles = new byte[getWidth() * getHeight()];
6768
this.loadTiles();
6869
} catch (IOException e) {
6970
e.printStackTrace();
7071
}
7172
}
7273

7374
private void loadTiles() {
74-
int[] tileColours = this.image.getRGB(0, 0, width, height, null, 0,
75-
width);
76-
for (int y = 0; y < height; y++) {
77-
for (int x = 0; x < width; x++) {
75+
int[] tileColours = this.image.getRGB(0, 0, getWidth(), getHeight(), null, 0,
76+
getWidth());
77+
for (int y = 0; y < getHeight(); y++) {
78+
for (int x = 0; x < getWidth(); x++) {
7879
tileCheck: for (Tile t : Tile.getTiles()) {
7980
if (t != null
80-
&& t.getLevelColour() == tileColours[x + y * width]) {
81-
this.tiles[x + y * width] = t.getId();
81+
&& t.getLevelColour() == tileColours[x + y * getWidth()]) {
82+
this.tiles[x + y * getWidth()] = t.getId();
8283
break tileCheck;
8384
}
8485
}
@@ -98,17 +99,17 @@ private void saveLevelToFile() {
9899

99100
@SuppressWarnings("unused")
100101
private void alterTile(int x, int y, Tile newTile) {
101-
this.tiles[x + y * width] = newTile.getId();
102+
this.tiles[x + y * getWidth()] = newTile.getId();
102103
image.setRGB(x, y, newTile.getLevelColour());
103104
}
104105

105106
private void generateLevel() {
106-
for (int y = 0; y < height; y++) {
107-
for (int x = 0; x < width; x++) {
107+
for (int y = 0; y < getHeight(); y++) {
108+
for (int x = 0; x < getWidth(); x++) {
108109
if (x * y % 10 < 7) {
109-
tiles[x + y * width] = Tile.getGrass().getId();
110+
tiles[x + y * getWidth()] = Tile.getGrass().getId();
110111
} else {
111-
tiles[x + y * width] = Tile.getStone().getId();
112+
tiles[x + y * getWidth()] = Tile.getStone().getId();
112113
}
113114
}
114115
}
@@ -144,23 +145,20 @@ public void renderTiles(Screen screen, int xOffset, int yOffset) {
144145
if (xOffset < 0) {
145146
xOffset = 0;
146147
}
147-
if (xOffset > ((width << 3) - screen.getWidth())) {
148-
xOffset = ((width << 3) - screen.getWidth());
148+
if (xOffset > ((getWidth() << 3) - screen.getWidth())) {
149+
xOffset = ((getWidth() << 3) - screen.getWidth());
149150
}
150151
if (yOffset < 0) {
151152
yOffset = 0;
152153
}
153-
if (yOffset > ((height << 3) - screen.getHeight())) {
154-
yOffset = ((height << 3) - screen.getHeight());
154+
if (yOffset > ((getHeight() << 3) - screen.getHeight())) {
155+
yOffset = ((getHeight() << 3) - screen.getHeight());
155156
}
156157

157158
screen.setOffset(xOffset, yOffset);
158159

159-
for (int y = (yOffset >> 3); y < (yOffset + screen.getHeight() >> 3) + 1; y++) {
160-
for (int x = (xOffset >> 3); x < (xOffset + screen.getWidth() >> 3) + 1; x++) {
161-
getTile(x, y).render(screen, this, x << 3, y << 3);
162-
}
163-
}
160+
Scene scene = new Scene(xOffset, yOffset, screen, this);
161+
scene.playerScene();
164162
}
165163

166164
public void renderEntities(Screen screen) {
@@ -176,10 +174,10 @@ public void renderProjectileEntities(Screen screen){
176174
}
177175

178176
public Tile getTile(int x, int y) {
179-
if (0 > x || x >= width || 0 > y || y >= height) {
177+
if (0 > x || x >= getWidth() || 0 > y || y >= getHeight()) {
180178
return Tile.getVoid();
181179
}
182-
return Tile.getTiles()[tiles[x + y * width]];
180+
return Tile.getTiles()[tiles[x + y * getWidth()]];
183181
}
184182

185183
public void addEntity(Entity entity) {
@@ -332,4 +330,20 @@ public List<Player> getPlayers(Entity e, int radius){
332330
return result;
333331
}
334332

333+
public int getWidth() {
334+
return width;
335+
}
336+
337+
public void setWidth(int width) {
338+
this.width = width;
339+
}
340+
341+
public int getHeight() {
342+
return height;
343+
}
344+
345+
public void setHeight(int height) {
346+
this.height = height;
347+
}
348+
335349
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.redomar.game.scenes;
2+
3+
import com.redomar.game.gfx.Screen;
4+
import com.redomar.game.level.LevelHandler;
5+
6+
public class Scene {
7+
8+
private int xOffset, yOffset;
9+
private Screen screen;
10+
private LevelHandler level;
11+
12+
public Scene(int xOffset, int yOffset, Screen screen, LevelHandler level){
13+
this.xOffset = xOffset;
14+
this.yOffset = yOffset;
15+
this.screen = screen;
16+
this.level = level;
17+
}
18+
19+
public void playerScene(){
20+
if (xOffset < 0) {
21+
xOffset = 0;
22+
}
23+
if (xOffset > ((level.getWidth() << 10) - screen.getWidth())) {
24+
xOffset = ((level.getWidth() << 30) - screen.getWidth());
25+
}
26+
if (yOffset < 0) {
27+
yOffset = 0;
28+
}
29+
if (yOffset > ((level.getHeight() << 3) - screen.getHeight())) {
30+
yOffset = ((level.getHeight() << 3) - screen.getHeight());
31+
}
32+
for (int y = (yOffset >> 3); y < (yOffset + screen.getHeight() >> 3) + 1; y++) {
33+
for (int x = (xOffset >> 3); x < (xOffset + screen.getWidth() >> 3) + 1; x++) {
34+
level.getTile(x, y).render(screen, level, x << 3, y << 3);
35+
}
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)