package net.figgle.minecraftplatformer;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
public class WorkBench {
public static Cell[] workBench = new Cell[Tile.workBenchLength * Tile.workBenchHeight];
public static Cell[] product = new Cell[Tile.productLength * Tile.productHeight];
private static boolean isHolding = false;
public static boolean isWorkBenchOpen = false;
public static int[] holdingID = Tile.air;
private int yOffSet = 150;
public WorkBench() {
int x = 0, y = 0;
for(int i = 0; i< workBench.length; i++) {
workBench[i] = new Cell(new Rectangle((Component.pixel.width >> 1) - ((Tile.workBenchLength * (Tile.invCellSize + Tile.invCellSpace)) >> 1) + (x * (Tile.invCellSize + Tile.invCellSpace)), Component.pixel.height - (Tile.invCellSize + Tile.invBorderSpace) - (Tile.workBenchHeight * (Tile.invCellSize + Tile.invCellSpace)) + (y * (Tile.invCellSize + Tile.invCellSpace)) - (yOffSet), Tile.invCellSize, Tile.invCellSize), Tile.air);
x++;
if(x == Tile.invLength) {
x = 0;
y++;
}
}
for(int i = 0; i< product.length; i++) {
product[i] = new Cell(new Rectangle((Component.pixel.width >> 1) - ((Tile.productLength * (Tile.invCellSize + Tile.invCellSpace)) >> 1) + (x * (Tile.invCellSize + Tile.invCellSpace)) + 58, Component.pixel.height - (Tile.invCellSize + Tile.invBorderSpace) - (Tile.productHeight * (Tile.invCellSize + Tile.invCellSpace)) + (y * (Tile.invCellSize + Tile.invCellSpace)) - (yOffSet + 118), Tile.invCellSize, Tile.invCellSize), Tile.air);
x++;
if(x == Tile.productLength) {
x = 0;
y++;
}
}
workBench[0].id = Tile.earth;
workBench[2].id = Tile.lava;
}
public static void click(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON1) {
if(isWorkBenchOpen) {
for(int i = 0; i < workBench.length; i++) {
if(workBench[i].contains(new Point(Component.mse.x / Component.pixelSize, Component.mse.y / Component.pixelSize))){
if(workBench[i].id != Tile.air && !isHolding) {
holdingID = workBench[i].id;
workBench[i].id = Tile.air;
isHolding = true;
} else if(isHolding && workBench[i].id == Tile.air) {
workBench[i].id = holdingID;
isHolding = false;
} else if (isHolding && workBench[i].id != Tile.air) {
int[] con = workBench[i].id;
workBench[i].id = holdingID;
holdingID = con;
}
}
} for(int i = 0; i < product.length; i++) {
if(product[i].contains(new Point(Component.mse.x / Component.pixelSize, Component.mse.y / Component.pixelSize))){
if(product[i].id != Tile.air && !isHolding) {
holdingID = product[i].id;
product[i].id = Tile.air;
isHolding = true;
} else if(isHolding && product[i].id == Tile.air) {
product[i].id = holdingID;
isHolding = false;
} else if (isHolding && product[i].id != Tile.air) {
int[] con = product[i].id;
product[i].id = holdingID;
holdingID = con;
}
}
}
for(int i = 0; i < Inventory.invBar.length; i++) {
if(Inventory.invBar[i].contains(new Point(Component.mse.x / Component.pixelSize, Component.mse.y / Component.pixelSize))){
if(Inventory.invBar[i].id != Tile.air && !isHolding) {
holdingID = Inventory.invBar[i].id;
Inventory.invBar[i].id = Tile.air;
isHolding = true;
} else if(isHolding && Inventory.invBar[i].id == Tile.air) {
Inventory.invBar[i].id = holdingID;
isHolding = false;
} else if (isHolding && Inventory.invBar[i].id != Tile.air) {
int[] con = Inventory.invBar[i].id;
Inventory.invBar[i].id = holdingID;
holdingID = con;
}
}
}
for(int i = 0; i < Inventory.invBag.length; i++) {
if(Inventory.invBag[i].contains(new Point(Component.mse.x / Component.pixelSize, Component.mse.y / Component.pixelSize))){
if(Inventory.invBag[i].id != Tile.air && !isHolding) {
holdingID = Inventory.invBag[i].id;
Inventory.invBag[i].id = Tile.air;
isHolding = true;
} else if(isHolding && Inventory.invBag[i].id == Tile.air) {
Inventory.invBag[i].id = holdingID;
isHolding = false;
} else if (isHolding && Inventory.invBag[i].id != Tile.air) {
int[] con = Inventory.invBag[i].id;
Inventory.invBag[i].id = holdingID;
holdingID = con;
}
}
}
}
}
}
public void checkRecipes() {
if(product[0].id == Tile.air) {
if(workBench[0].id == Tile.earth && workBench[1].id == Tile.lava) {
if(holdingID == Tile.sand) {
workBench[0].id = Tile.air;
workBench[1].id = Tile.air;
product[0].id = Tile.air;
}
if(holdingID != Tile.sand) {
product[0].id = Tile.sand;
}
} else {
product[0].id = Tile.air;
}
}
}
public void render(Graphics g) {
if(isWorkBenchOpen) {
for(int i=0;i<workBench.length;i++) {
workBench[i].render(g, false);
}
for(int i=0;i<product.length;i++) {
product[i].render(g, false);
}
}
if(isHolding) {
g.drawImage(Tile.tileset_terrain, (Component.mse.x / Component.pixelSize) - (Tile.invCellSize / 2) + Tile.invItemBorder, (Component.mse.y / Component.pixelSize) - (Tile.invCellSize / 2) + Tile.invItemBorder, (Component.mse.x / Component.pixelSize) - (Tile.invCellSize / 2) + Tile.invCellSize - Tile.invItemBorder, (Component.mse.y / Component.pixelSize) - (Tile.invCellSize / 2) + Tile.invCellSize - Tile.invItemBorder, holdingID[0] * Tile.tileSize, holdingID[1] * Tile.tileSize, holdingID[0] * Tile.tileSize + Tile.tileSize, holdingID[1] * Tile.tileSize+ Tile.tileSize, null);
}
}
}
[B][U]Inventory class[U][B][/B][/U][/U][/B]
package net.figgle.minecraftplatformer;
import java.awt.*;
import java.awt.event.MouseEvent;
public class Inventory {
public static Cell[] invBar = new Cell[Tile.invLength];
public static Cell[] invBag = new Cell[Tile.invLength * Tile.invHeight];
public static boolean isOpen = false;
public static boolean isHolding = false;
public static int selected = 0;
public static int[] holdingID = Tile.air;
public Inventory() {
for(int i=0;i<invBar.length;i++) {
invBar[i] = new Cell(new Rectangle((Component.pixel.width >> 1) - ((Tile.invLength * (Tile.invCellSize + Tile.invCellSpace)) >> 1) + (i * (Tile.invCellSize + Tile.invCellSpace)), Component.pixel.height - (Tile.invCellSize+ Tile.invBorderSpace), Tile.invCellSize, Tile.invCellSize), Tile.air);
}
int x = 0, y = 0;
for(int i = 0; i< invBag.length; i++) {
invBag[i] = new Cell(new Rectangle((Component.pixel.width >> 1) - ((Tile.invLength * (Tile.invCellSize + Tile.invCellSpace)) >> 1) + (x * (Tile.invCellSize + Tile.invCellSpace)), Component.pixel.height - (Tile.invCellSize + Tile.invBorderSpace) - (Tile.invHeight * (Tile.invCellSize + Tile.invCellSpace)) + (y * (Tile.invCellSize + Tile.invCellSpace)), Tile.invCellSize, Tile.invCellSize), Tile.air);
x++;
if(x == Tile.invLength) {
x = 0;
y++;
}
}
invBar[0].id = Tile.earth;
invBar[1].id = Tile.grass;
invBar[2].id = Tile.tnt;
invBar[3].id = Tile.workBench;
invBar[4].id = Tile.leaves;
invBar[5].id = Tile.wood;
invBar[6].id = Tile.sand;
invBar[7].id = Tile.lava;
}
public static void click(MouseEvent e) {
if(e.getButton() == 1) {
if(isOpen) {
for(int i = 0; i < invBar.length; i++) {
if(invBar[i].contains(new Point(Component.mse.x / Component.pixelSize, Component.mse.y / Component.pixelSize))){
if(invBar[i].id != Tile.air && !isHolding) {
holdingID = invBar[i].id;
invBar[i].id = Tile.air;
isHolding = true;
} else if(isHolding && invBar[i].id == Tile.air) {
invBar[i].id = holdingID;
isHolding = false;
} else if (isHolding && invBar[i].id != Tile.air) {
int[] con = invBar[i].id;
invBar[i].id = holdingID;
holdingID = con;
}
}
}
for(int i = 0; i < invBag.length; i++) {
if(invBag[i].contains(new Point(Component.mse.x / Component.pixelSize, Component.mse.y / Component.pixelSize))){
if(invBag[i].id != Tile.air && !isHolding) {
holdingID = invBag[i].id;
invBag[i].id = Tile.air;
isHolding = true;
} else if(isHolding && invBag[i].id == Tile.air) {
invBag[i].id = holdingID;
isHolding = false;
} else if (isHolding && invBag[i].id != Tile.air) {
int[] con = invBag[i].id;
invBag[i].id = holdingID;
holdingID = con;
}
}
}
}
}
}
public void render(Graphics g) {
for(int i=0;i<invBar.length;i++) {
boolean isSelected = false;
if(i == selected) {
isSelected = true;
}
invBar[i].render(g, isSelected);
}
if(isOpen) {
for(int i=0;i<invBag.length;i++) {
invBag[i].render(g, false);
}
}
if(isHolding) {
g.drawImage(Tile.tileset_terrain, (Component.mse.x / Component.pixelSize) - (Tile.invCellSize / 2) + Tile.invItemBorder, (Component.mse.y / Component.pixelSize) - (Tile.invCellSize / 2) + Tile.invItemBorder, (Component.mse.x / Component.pixelSize) - (Tile.invCellSize / 2) + Tile.invCellSize - Tile.invItemBorder, (Component.mse.y / Component.pixelSize) - (Tile.invCellSize / 2) + Tile.invCellSize - Tile.invItemBorder, holdingID[0] * Tile.tileSize, holdingID[1] * Tile.tileSize, holdingID[0] * Tile.tileSize + Tile.tileSize, holdingID[1] * Tile.tileSize+ Tile.tileSize, null);
}
}
}
[U][B]Component class[U][B][/B][/U][/B][/U]
package net.figgle.minecraftplatformer;
import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class Component extends Applet implements Runnable {
private static final long serialVersionUID = 1L;
public static byte pixelSize = 2;
public static int moveFromBorder = 0;
public static double sX = moveFromBorder, sY = moveFromBorder;
public static double dir = 0;
public static int width = 700;
public static int height = 560;
public int timer = 0;
public byte movingTimer = 0;
public static Dimension realSize = new Dimension(0, 0);
public static Dimension size = new Dimension(700, 560);
public static Dimension pixel = new Dimension(size.width / pixelSize, size.height / pixelSize);
public static Point mse = new Point(0, 0);
public static String name = "Minegeist v1.0.0 Alpha ";
public static String deathText = "You died!";
public static boolean isRunning = false;
public static boolean isMoving = false;
public static boolean isJumping = false;
public static boolean isMouseLeft = false;
public static boolean isMouseRight = false;
private Image screen;
public int fps;
public int TotalTime;
public static Level level;
public static Character character;
public static Inventory inventory;
public static WorkBench workBench;
public static Sky sky;
public static ArrayList<Mob> mob = new ArrayList<Mob>();
public static Spawner spawner;
public static Checker checker;
public Component() {
setPreferredSize(size);
addKeyListener(new Listening());
addMouseListener(new Listening());
addMouseMotionListener(new Listening());
addMouseWheelListener(new Listening());
}
public void start() {
frame = new JFrame();
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image cursor = toolkit.getImage("res/Cursors/cursor.png");
Point cursorHotSpot = new Point(17, 17);
Cursor customCursor = toolkit.createCustomCursor(cursor, cursorHotSpot, "Cursor");
frame.setCursor(customCursor);
realSize = new Dimension(frame.getWidth(), frame.getHeight());
frame.setTitle(name);
frame.setSize(new Dimension(WIDTH, HEIGHT));
frame.add(this);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
frame.setIconImage(new ImageIcon("res/windowIcon.png").getImage());
//Defining objects etc.
new Tile(); //Loading images
level = new Level();
character = new Character(Tile.tileSize, Tile.tileSize*2);
inventory = new Inventory();
sky = new Sky();
workBench = new WorkBench();
spawner = new Spawner();
checker = new Checker();
//starting le game loop
isRunning = true;
new Thread(this).start();
}
public void stop() {
isRunning = false;
}
private static JFrame frame;
public static void main(String args[]) {
Component component = new Component();
new Menu(component);
}
public void tick() {
if(frame.getWidth()!= realSize.width||frame.getHeight()!= realSize.height){
frame.pack();
}
character.tick();
level.tick((int) sX, (int) sY, (pixel.width / Tile.tileSize) + 2, (pixel.height / Tile.tileSize) + 2);
sky.tick();
checker.tick();
workBench.checkRecipes();
for(int i = 0; i < mob.toArray().length; i++) {
mob.get(i).tick();
}
}
public void render() {
Graphics g = screen.getGraphics();
//Drawing things
sky.render(g);
level.render(g, (int) sX, (int) sY,(pixel.width / Tile.tileSize) + 2, (pixel.height / Tile.tileSize) + 2);
character.render(g);
for(int i = 0; i < mob.toArray().length; i++) {
mob.get(i).render(g);
}
inventory.render(g);
workBench.render(g);
g.setColor(Color.GREEN);
g.drawString(fps + " fps", 1, 10);
g.drawString("X: " + Math.round(sX/18), 315, 10);
g.drawString("Y: " + Math.round(sY/18), 315, 20);
if(character.isDead){
g.setColor(Color.BLACK);
g.fillRect(0, 0, width, height);
g.setColor(Color.RED);
g.setFont(new Font("Courier New", Font.BOLD, 24));
g.drawString(deathText, 80, 135);
}
if(timer < 250) {
timer++;
movingTimer++;
g.setColor(Color.BLACK);
g.fillRect(0, 0, width, height);
if(movingTimer > 10) {
g.drawImage(Tile.movingCharacter, timer + 35, 150, null);
} else {
g.drawImage(Tile.movingCharacter2, timer + 35, 150, null);
}
if(movingTimer > 20) {
movingTimer = 0;
}
g.setColor(Color.WHITE);
g.setFont(new Font("Courier New", Font.BOLD, 25));
g.drawString("Loading...", 125, 80);
g.setColor(Color.GREEN) ;
g.fillRect(50, 100, timer, 40);
g.setColor(Color.BLUE);
g.drawRect(50, 100, 250, 40);
}
g = getGraphics();
g.drawImage(screen, 0, 0, size.width, size.height, 0, 0, pixel.width, pixel.height, null);
g.dispose();
}
public void run() {
screen = createVolatileImage(pixel.width, pixel.height);
int frames = 0;
double nonProcessedSeconds = 0;
long previousTime = System.nanoTime();
double secondsPerTick = 1 / 60.0;
int TickCount = 0;
boolean hasTicked = false;
int TotalTime = 0;
while(isRunning) {
long currentTime = System.nanoTime();
long passedTime = currentTime - previousTime;
previousTime = currentTime;
nonProcessedSeconds += passedTime / 1000000000.0;
while(nonProcessedSeconds > secondsPerTick) {
tick();
nonProcessedSeconds -= secondsPerTick;
hasTicked = true;
TickCount++;
if(TickCount % 60 == 0) {
previousTime += 1000;
fps = frames;
frames = 0;
}
}
if(hasTicked){
frames++;
}
render();
frames++;
frames++;
requestFocus();
tick();
render();
try {
Thread.sleep(5);
} catch(Exception e) { }
}
}
}