Skip to content

Commit 8744a4e

Browse files
committed
Added new entity type: Tree and Spruce Tree
First addition of trees- not yet solid Minor Corrections of commenting
1 parent 2037d48 commit 8744a4e

File tree

6 files changed

+174
-24
lines changed

6 files changed

+174
-24
lines changed

res/sprite_sheet.png

-1.36 KB
Loading

src/com/redomar/game/Game.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.redomar.game.entities.Dummy;
55
import com.redomar.game.entities.Player;
66
import com.redomar.game.entities.Vendor;
7+
import com.redomar.game.entities.trees.Spruce;
78
import com.redomar.game.gfx.Screen;
89
import com.redomar.game.gfx.SpriteSheet;
910
import com.redomar.game.level.LevelHandler;
@@ -79,6 +80,7 @@ public class Game extends Canvas implements Runnable {
7980
private Player player;
8081
private Dummy dummy; // Dummy NPC follows the player around
8182
private Vendor vendor; // Vendor NPC exhibits random movement and is only found on cutom_level
83+
private Spruce spruce; // Tree -- Spruce
8284
private Font font = new Font(); // Font object capable of displaying 2 fonts: Arial and Segoe UI
8385
private String nowPlaying;
8486
private boolean notActive = true;
@@ -215,6 +217,8 @@ public void setMap(String Map_str) {
215217
setPlayer(new Player(level, 100, 100, input,
216218
getJdata_UserName(), shirtCol, faceCol));
217219
level.addEntity(player);
220+
spruce = new Spruce(level, 70,170, 2 );
221+
level.addEntity(spruce);
218222
}
219223

220224
public static void setMap(int map) {
@@ -545,7 +549,7 @@ public void render() {
545549
* whether it is running in developer mode, or if the application is closing.
546550
*/
547551
private void status(Graphics g, boolean TerminalMode, boolean TerminalQuit) {
548-
if (TerminalMode == true) { // If running in developer mode
552+
if (TerminalMode) { // If running in developer mode
549553
g.setColor(Color.CYAN);
550554
g.drawString("JavaGame Stats", 0, 10); // Display "JavaGame Stats" in cyan at the bottom left of the screen
551555
g.drawString("FPS/TPS: " + fps + "/" + tps, 0, 25); // Display the FPS and TPS in cyan directly above "JavaGame Stats"
@@ -563,14 +567,16 @@ private void status(Graphics g, boolean TerminalMode, boolean TerminalQuit) {
563567
g.setColor(Color.CYAN);
564568
g.fillRect(getMouse().getX() - 12, getMouse().getY() - 12, 24, 24);
565569
}
566-
if (TerminalQuit == true) { // If the game is shutting off
567-
g.setColor(Color.BLACK);
568-
g.fillRect(0, 0, getWidth(), getHeight()); // Make the screen fully black
569-
g.setColor(Color.RED);
570-
g.drawString("Shutting down the Game", (getWidth() / 2) - 70, // Display "Shutting down the Game" in red in the middle of the screen
571-
(getHeight() / 2) - 8);
572-
g.dispose(); // Free up memory for graphics
570+
// If the game is shutting off
571+
if (!TerminalQuit) {
572+
return;
573573
}
574+
g.setColor(Color.BLACK);
575+
g.fillRect(0, 0, getWidth(), getHeight()); // Make the screen fully black
576+
g.setColor(Color.RED);
577+
g.drawString("Shutting down the Game", (getWidth() / 2) - 70, // Display "Shutting down the Game" in red in the middle of the screen
578+
(getHeight() / 2) - 8);
579+
g.dispose(); // Free up memory for graphics
574580
}
575581

576582
public WindowHandler getWindow() {
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.redomar.game.entities.trees;
2+
3+
import com.redomar.game.gfx.Colours;
4+
import com.redomar.game.gfx.Screen;
5+
import com.redomar.game.level.LevelHandler;
6+
7+
public class Spruce extends Tree {
8+
9+
/**
10+
* Spruce Tree by MOHAMED OMAR
11+
*
12+
*/
13+
14+
//BIT MASK variables in Bits. Used to select bits by using comparing.
15+
private final int BIT_MASK_MOVE_RIGHT = 0b0001;
16+
private final int BIT_MASK_MOVE_UP_1 = 0b0010;
17+
private final int BIT_MASK_MOVE_UP_2 = 0b0100;
18+
19+
private int scale;
20+
21+
/**
22+
* Spruce tree
23+
* @param level LevelHandler level which spruces spawns
24+
* @param x X co-ordinate
25+
* @param y Y co-ordinate
26+
* @param scale Size of tree
27+
*/
28+
public Spruce(LevelHandler level, double x, double y, int scale) {
29+
super(level, x, y);
30+
this.setX((int)x);
31+
this.setY((int)y);
32+
this.setName("Spruce_Tree");
33+
this.setScale(scale);
34+
}
35+
36+
@Override
37+
public void tick() {
38+
39+
}
40+
41+
/**
42+
* This Renders the spruce tree
43+
*
44+
* Spruce is 3x2 on the sprite sheet
45+
*
46+
* 0 1
47+
* +---------+
48+
* 6 |0100|0101|
49+
* 7 |0010|0011|
50+
* 8 |0000|0001|
51+
* +---------+
52+
*
53+
* @param screen Screen to render on.
54+
*/
55+
public void render(Screen screen) {
56+
57+
//Spruce uses 6 tiles from sprite sheet.
58+
int spruceSize = 0;
59+
60+
//This will go through a while loop for each 8*8 section of the spruce
61+
while (spruceSize < 6){
62+
int right = 0;
63+
int up1 = 0;
64+
int up2 = 0;
65+
int tileX = 0;
66+
int tileY = 4;
67+
68+
69+
//BIT MASK for all the right sided sprites. comparing it to 0b0001 (last bit).
70+
if ((spruceSize & BIT_MASK_MOVE_RIGHT) > 0){
71+
//MOVE RIGHT
72+
right = scale * 8;
73+
tileX = tileX + 1;
74+
}
75+
76+
//it will only move up 1 time if it does not move up twice.
77+
if ((spruceSize & BIT_MASK_MOVE_UP_2) > 0){
78+
//MOVE UP 2 TIMES
79+
up2 = scale * 8;
80+
tileY = tileY -2;
81+
} else if ((spruceSize & BIT_MASK_MOVE_UP_1) > 0){
82+
//MOVE UP ONCE
83+
up1 = scale * 8;
84+
tileY = tileY -1;
85+
}
86+
87+
//rendering to game. Inline if statement ensures that tree trunk has a unique colour
88+
screen.render((int)getX()+right,((int)getY() - up1) - up2*2,(tileY*32+tileX), Colours.get(-1,020,241, spruceSize > 0b0001? 251: 110), 0x00, scale);
89+
90+
spruceSize++;
91+
}
92+
93+
}
94+
95+
//Some getters and setters
96+
public int getScale() {
97+
return scale;
98+
}
99+
100+
public void setScale(int scale) {
101+
this.scale = scale;
102+
}
103+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.redomar.game.entities.trees;
2+
3+
import com.redomar.game.entities.Entity;
4+
import com.redomar.game.level.LevelHandler;
5+
6+
public abstract class Tree extends Entity {
7+
8+
private final LevelHandler level;
9+
/**
10+
* Tree abstract class
11+
* -- Used by Spruce Class
12+
*/
13+
14+
//Position variables
15+
protected final double x;
16+
protected final double y;
17+
18+
/**
19+
* Constructor for tree entities
20+
* @param level LevelHandler in which tree spawns
21+
* @param x X co-ordinate
22+
* @param y Y co-ordinate
23+
*/
24+
Tree(LevelHandler level, double x, double y) {
25+
super(level);
26+
this.level = level;
27+
this.x = x;
28+
this.y = y;
29+
}
30+
31+
}

src/com/redomar/game/gfx/Screen.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public Screen(int width, int height, SpriteSheet sheet) {
3333

3434
setPixels(new int[width * height]);
3535
}
36+
3637
public static int getMapWidthMask() {
3738
return MAP_WIDTH_MASK;
3839
}
@@ -45,7 +46,7 @@ public static int getMapWidthMask() {
4546
* @param yPos Y Postion of subject
4647
* @param tile tile location. e.g 7 * 32 + 1 is the oblong bullet on the 7th row 2nd colomn
4748
* @param colour Using established colouring nomanclature. i.e. use com.redomar.game.gfx.Colours
48-
* @param mirrorDir flip Direction: 0x00 first 0 flip verticle, second 0 flip horizontal. 0x01 horizontal
49+
* @param mirrorDir flip Direction: 0x01 flip verticle, 0x02 flip horizontal.
4950
* @param scale Scale
5051
*/
5152
public void render(int xPos, int yPos, int tile, int colour, int mirrorDir,

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

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,13 @@ public class LevelHandler {
2525
private byte[] tiles;
2626
private int width;
2727
private int height;
28-
private List<Entity> entities = new ArrayList<Entity>();
29-
private List<Entity> entities_p = new ArrayList<Entity>();
28+
private List<Entity> entities;
29+
private List<Entity> entities_p;
3030
private String imagePath;
3131
private BufferedImage image;
3232
private Printing print;
3333

34-
private Comparator<Node> nodeSorter = new Comparator<Node>() {
35-
36-
public int compare(Node n0, Node n1) {
37-
if(n1.fCost < n0.fCost) return +1;
38-
if(n1.fCost > n0.fCost) return -1;
39-
return 0;
40-
}
41-
42-
};
34+
private Comparator<Node> nodeSorter;
4335

4436
public LevelHandler(String imagePath) {
4537

@@ -54,6 +46,17 @@ public LevelHandler(String imagePath) {
5446
}
5547

5648
print = new Printing();
49+
entities = new ArrayList<Entity>();
50+
entities_p = new ArrayList<Entity>();
51+
nodeSorter = new Comparator<Node>() {
52+
53+
public int compare(Node n0, Node n1) {
54+
if(n1.fCost < n0.fCost) return +1;
55+
if(n1.fCost > n0.fCost) return -1;
56+
return 0;
57+
}
58+
59+
};
5760
}
5861

5962
private void loadLevelFromFile() {
@@ -73,13 +76,12 @@ private void loadTiles() {
7376
getWidth());
7477
for (int y = 0; y < getHeight(); y++) {
7578
for (int x = 0; x < getWidth(); x++) {
76-
tileCheck: for (Tile t : Tile.getTiles()) {
79+
for (Tile t : Tile.getTiles())
7780
if (t != null
7881
&& t.getLevelColour() == tileColours[x + y * getWidth()]) {
7982
this.tiles[x + y * getWidth()] = t.getId();
80-
break tileCheck;
83+
break;
8184
}
82-
}
8385
}
8486
}
8587
}
@@ -191,12 +193,19 @@ public void addProjectileEntities(Entity entity) {
191193
this.getProjectileEntities().add(entity);
192194
}
193195

196+
197+
/**
198+
* Removes the entity that is passed through
199+
* @param entity Specifies which entity needs to be removed
200+
*/
194201
public void removeEntity(Entity entity) {
195-
this.entities.remove(entity);
202+
if(entity == null) return;
196203
print.print("Removed "+entity.getName()+" Entity", PrintTypes.LEVEL);
204+
this.entities.remove(entity);
197205
try {
198206
Thread.sleep(100);
199207
} catch (InterruptedException e) {
208+
print.print("Interruption error: "+ e, PrintTypes.ERROR);
200209
e.printStackTrace();
201210
}
202211
}

0 commit comments

Comments
 (0)