Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ocelot.game;
- import java.awt.BorderLayout;
- import java.awt.Canvas;
- import java.awt.Color;
- import java.awt.Cursor;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Point;
- import java.awt.Toolkit;
- import java.awt.image.BufferStrategy;
- import java.awt.image.BufferedImage;
- import java.awt.image.DataBufferInt;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- import javax.swing.JFrame;
- import javax.swing.UIManager;
- import com.ocelot.game.audio.JukeBox;
- import com.ocelot.game.entities.Entity;
- import com.ocelot.game.entities.EntityBread;
- import com.ocelot.game.entities.EntityPlayer;
- import com.ocelot.game.entities.EntityProjectile;
- import com.ocelot.game.entities.EntityRabbit;
- import com.ocelot.game.entities.EntityTree;
- import com.ocelot.game.entities.EntityZombie;
- import com.ocelot.game.entities.PlayerMP;
- import com.ocelot.game.gfx.Font;
- import com.ocelot.game.gfx.Screen;
- import com.ocelot.game.gfx.SpriteColor;
- import com.ocelot.game.gfx.gui.Gui;
- import com.ocelot.game.gfx.gui.GuiGameOver;
- import com.ocelot.game.gfx.gui.GuiHotbar;
- import com.ocelot.game.gfx.gui.GuiInventory;
- import com.ocelot.game.gfx.gui.GuiLoading;
- import com.ocelot.game.gfx.gui.Hud;
- import com.ocelot.game.handlers.Cursors;
- import com.ocelot.game.handlers.InputHandler;
- import com.ocelot.game.handlers.WindowHandler;
- import com.ocelot.game.launcher.Configuration;
- import com.ocelot.game.launcher.Launcher;
- import com.ocelot.game.level.Level;
- import com.ocelot.game.level.tiles.Tile;
- import com.ocelot.game.net.GameClient;
- import com.ocelot.game.net.GameServer;
- import com.ocelot.game.net.packet.Packet00Login;
- import com.ocelot.game.utils.LoadingUtils;
- import com.ocelot.game.utils.Logger;
- /**
- * @author Ocelot5836
- */
- @SuppressWarnings("serial")
- public class Game extends Canvas implements Runnable {
- public static final int WIDTH = 320;
- public static final int HEIGHT = WIDTH / 12 * 9;
- public static final int SCALE = 3;
- private static Configuration config = new Configuration("settings");
- public static boolean debug = false;
- public static int debugSelction = 1;
- public int fps;
- public int updates;
- public static int score = 0;
- public static double maxUpdates = 60D;
- public static long sleeps = 3;
- private static Toolkit toolkit = Toolkit.getDefaultToolkit();
- public static Cursor DefaultCursor;
- public static Cursor TyCursor;
- private JFrame frame;
- public static boolean paused = false;
- public boolean running = false;
- public int tickCount = 0;
- public static int time = 0;
- public static int tileSelected = 0;
- public static int offTileSelected = 0;
- public static int musicVolume = 0;
- public static int sfxVolume = 0;
- private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
- private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
- private int[] colors = new int[6 * 6 * 6];
- public static int cursorTileX;
- public static int cursorTileY;
- public static int cursorX;
- public static int cursorY;
- private Screen screen;
- public Gui gui;
- public InputHandler input;
- public WindowHandler window;
- public Level level;
- private static Launcher launcher;
- public EntityPlayer player;
- private GameClient socketClient;
- private GameServer socketServer;
- public static List<EntityRabbit> rabbits;
- public static List<EntityZombie> zombies;
- public static List<EntityBread> breads;
- public static int difficultySelection;
- public static int numRabbits;
- public static String worldName;
- public static String username;
- public static String ip;
- public static int port;
- public static boolean runServer;
- public Game() {
- frame = new JFrame(Assets.NAME + " " + Assets.VERSION);
- frame.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
- frame.setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
- frame.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
- frame.setCursor(Cursors.get(Launcher.selectedCursor));
- frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
- frame.setLayout(new BorderLayout());
- frame.pack();
- frame.add(this, BorderLayout.CENTER);
- frame.setResizable(false);
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- if (runServer)
- socketServer = new GameServer(this, port);
- socketClient = new GameClient(this, ip, port);
- }
- public static Launcher getLauncherInstance() {
- if (launcher == null) {
- launcher = new Launcher();
- }
- return launcher;
- }
- public void init() {
- int index = 0;
- for (int r = 0; r < 6; r++) {
- for (int g = 0; g < 6; g++) {
- for (int b = 0; b < 6; b++) {
- int rr = (r * 255 / 5);
- int gg = (g * 255 / 5);
- int bb = (b * 255 / 5);
- colors[index++] = rr << 16 | gg << 8 | bb;
- }
- }
- }
- JukeBox.init();
- debug = debugSelction == 0 ? true : false;
- screen = new Screen(WIDTH, HEIGHT);
- input = new InputHandler();
- window = new WindowHandler(this, frame);
- this.addFocusListener(input);
- this.addKeyListener(input);
- this.addMouseListener(input);
- this.addMouseMotionListener(input);
- this.addMouseWheelListener(input);
- frame.addWindowListener(window);
- frame.addWindowFocusListener(window);
- Level.name = worldName;
- level = new Level(this);
- Logger.init(this);
- Tile.init();
- initMobs();
- gui = new Gui(this, input, level, player);
- }
- public void initMobs() {
- player = new PlayerMP(level, this, 100, 100, input, username, null, -1);
- level.add(player);
- Packet00Login loginPacket = new Packet00Login(player.getUsername());
- if (socketServer != null) {
- socketServer.addConnection((PlayerMP) player, loginPacket);
- }
- loginPacket.writeData(socketClient);
- rabbits = new ArrayList<EntityRabbit>();
- for (int i = 0; i < numRabbits; i++) {
- Random x = new Random();
- Random y = new Random();
- EntityRabbit temp = new EntityRabbit(level, this, x.nextInt(level.width * 8), y.nextInt(level.height * 8));
- rabbits.add(temp);
- }
- breads = new ArrayList<EntityBread>();
- if (difficultySelection != 0) {
- for (int i = 0; i < 100; i++) {
- Random x = new Random();
- Random y = new Random();
- EntityBread temp = new EntityBread(level, x.nextInt(level.width * 8), y.nextInt(level.height * 8));
- breads.add(temp);
- }
- }
- zombies = new ArrayList<EntityZombie>();
- int numMobRuns = 0;
- if (difficultySelection != 0) {
- if (difficultySelection == 1) {
- numMobRuns = 20;
- }
- if (difficultySelection == 2) {
- numMobRuns = 40;
- }
- if (difficultySelection == 3) {
- numMobRuns = 60;
- }
- for (int i = 0; i < numMobRuns; i++) {
- Random x = new Random();
- Random y = new Random();
- EntityZombie temp = new EntityZombie(level, this, x.nextInt(level.width * 8), y.nextInt(level.height * 8));
- zombies.add(temp);
- }
- }
- for (int i = 0; i < 50; i++) {
- Random x = new Random();
- Random y = new Random();
- if (level.getTile(x.nextInt(level.width * 8) / 8, y.nextInt(level.height * 8) / 8) == Tile.GRASS)
- level.add(new EntityTree(level, x.nextInt(level.width * 8), y.nextInt(level.height * 8)));
- }
- for (EntityRabbit rabbit : rabbits) {
- level.add(rabbit);
- }
- for (EntityZombie zombie : zombies) {
- level.add(zombie);
- }
- for (EntityBread bread : breads) {
- level.add(bread);
- }
- }
- public void start() {
- running = true;
- new Thread(this, "game").start();
- if (runServer)
- socketServer.start();
- socketClient.start();
- }
- public void stop() {
- Logger.info("Stopping...");
- if (level != null)
- level.save();
- running = false;
- System.exit(0);
- }
- public void run() {
- long lastTime = System.nanoTime();
- double nsPerTick = 1000000000D / maxUpdates;
- int ticks = 0;
- int frames = 0;
- long lastTimer = System.currentTimeMillis();
- double delta = 0;
- init();
- requestFocus();
- InputHandler.placing = false;
- renderLoadingScreen();
- renderLoadingScreen();
- JukeBox.load("/audio/music/music", "music");
- JukeBox.load("/audio/sfx/gunshot", "fire");
- JukeBox.load("/audio/sfx/hurt", "hurt");
- JukeBox.load("/audio/sfx/pop", "pop");
- JukeBox.load("/audio/sfx/fart", "fart");
- JukeBox.loop("music");
- while (running) {
- long now = System.nanoTime();
- delta += (now - lastTime) / nsPerTick;
- lastTime = now;
- while (delta >= 1) {
- ticks++;
- tick();
- delta -= 1;
- }
- try {
- Thread.sleep(sleeps);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- frames++;
- render();
- if (System.currentTimeMillis() - lastTimer >= 1000) {
- lastTimer += 1000;
- fps = frames;
- updates = ticks;
- level.save();
- frames = 0;
- ticks = 0;
- }
- }
- }
- int index = 0;
- public void tick() {
- time++;
- JukeBox.setVolume("music", musicVolume - 50);
- if (musicVolume <= 0)
- JukeBox.stop("music");
- for (String s : JukeBox.clips.keySet()) {
- JukeBox.setVolume(s, sfxVolume - 50);
- if (sfxVolume <= 0)
- JukeBox.stop(s);
- }
- if (!paused) {
- if (time % 60 == 0) {
- if (fps > 120) {
- sleeps++;
- }
- if (fps < 110) {
- sleeps--;
- }
- if (sleeps <= 0) {
- sleeps = 0;
- }
- }
- tickCount++;
- if (!player.isRemoved()) {
- player.tick();
- level.tick();
- }
- for (int i = 0; i < level.entities.size(); i++) {
- if (level.entities.get(i).isRemoved()) {
- level.entities.remove(i);
- i--;
- }
- }
- for (int i = 0; i < level.projectiles.size(); i++) {
- if (level.projectiles.get(i).isRemoved()) {
- level.projectiles.remove(i);
- i--;
- }
- }
- for (int i = 0; i < level.particles.size(); i++) {
- if (level.particles.get(i).isRemoved()) {
- level.particles.remove(i);
- i--;
- }
- }
- if (!player.isRemoved())
- tileCheck();
- }
- }
- private void tileCheck() {
- cursorTileX = ((screen.xOffset * SCALE) + InputHandler.mouseCX) / (SCALE * 8);
- cursorTileY = ((screen.yOffset * SCALE) + InputHandler.mouseCY) / (SCALE * 8);
- cursorX = ((screen.xOffset * SCALE) + InputHandler.mouseCX) / SCALE;
- cursorY = ((screen.yOffset * SCALE) + InputHandler.mouseCY) / SCALE;
- if (InputHandler.mouseButton == 2)
- tileSelected = level.getTile(cursorTileX, cursorTileY).getId();
- if (player.canEditTile(cursorTileX, cursorTileY) && InputHandler.placing && InputHandler.mouseButton == 1)
- level.alterTile(cursorTileX, cursorTileY, Tile.tiles[tileSelected]);
- }
- public List<Entity> getEntities(Entity e, int radius) {
- List<Entity> result = new ArrayList<Entity>();
- double ex = (e.getX());
- double ey = (e.getY());
- for (int i = 0; i < level.entities.size(); i++) {
- Entity entity = level.entities.get(i);
- int x = ((int) entity.getX());
- int y = ((int) entity.getY());
- double dx = Math.abs(x - ex);
- double dy = Math.abs(y - ey);
- double distance = Math.sqrt((dx * dx) + (dy * dy));
- if (distance <= radius)
- result.add(entity);
- }
- return result;
- }
- public List<Entity> getEntity(Entity e, int radius) {
- List<Entity> entities = getEntities(e, radius);
- List<Entity> result = new ArrayList<Entity>();
- for (int i = 0; i < entities.size(); i++) {
- if (entities.get(i) == e)
- result.add(entities.get(i));
- }
- return result;
- }
- public List<EntityProjectile> getProjectiles(Entity e, int radius) {
- List<EntityProjectile> result = new ArrayList<EntityProjectile>();
- double ex = (e.getX());
- double ey = (e.getY());
- for (int i = 0; i < level.projectiles.size(); i++) {
- EntityProjectile projectile = level.projectiles.get(i);
- int x = ((int) projectile.getProjectileX());
- int y = ((int) projectile.getProjectileY());
- double dx = Math.abs(x - ex);
- double dy = Math.abs(y - ey);
- double distance = Math.sqrt((dx * dx) + (dy * dy));
- if (distance <= radius)
- result.add(projectile);
- }
- return result;
- }
- public List<EntityPlayer> getPlayers(Entity e, int radius) {
- List<EntityPlayer> result = new ArrayList<EntityPlayer>();
- double ex = (e.getX());
- double ey = (e.getY());
- for (int i = 0; i < 1; i++) {
- EntityPlayer entity = player;
- int x = ((int) entity.getX());
- int y = ((int) entity.getY());
- double dx = Math.abs(x - ex);
- double dy = Math.abs(y - ey);
- double distance = Math.sqrt((dx * dx) + (dy * dy));
- if (distance <= radius)
- result.add(entity);
- }
- return result;
- }
- public void renderLoadingScreen() {
- BufferStrategy bs = getBufferStrategy();
- if (bs == null) {
- createBufferStrategy(3);
- return;
- }
- Graphics g = bs.getDrawGraphics();
- GuiLoading loading = new GuiLoading();
- gui.add(loading);
- gui.renderGui(screen, loading);
- for (int y = 0; y < screen.height; y++) {
- for (int x = 0; x < screen.width; x++) {
- int colorCode = screen.pixels[x + y * screen.width];
- if (colorCode < 255)
- pixels[x + y * WIDTH] = colors[colorCode];
- }
- }
- g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
- gui.renderGuiOverlays(screen, g);
- g.dispose();
- Gui.guis.clear();
- bs.show();
- }
- public void render() {
- BufferStrategy bs = getBufferStrategy();
- if (bs == null) {
- createBufferStrategy(3);
- return;
- }
- Graphics g = bs.getDrawGraphics();
- int xOffset = ((int) player.x) - (screen.width / 2);
- int yOffset = ((int) player.y) - (screen.height / 2);
- if (!player.isRemoved()) {
- level.renderTiles(screen, xOffset, yOffset);
- screen.render(cursorTileX * 8, cursorTileY * 8, screen.entitiesSheet, 1, new SpriteColor(-1, 000, -1, -1), 0x00, 1);
- level.renderEntities(screen, this);
- // gui.add(new HudTile());
- if (input.inventory.isPressed()) {
- gui.add(new GuiInventory());
- paused = true;
- } else {
- paused = false;
- }
- Font.render(Assets.VERSION, screen, screen.xOffset + (WIDTH - (Assets.VERSION.length() * 8)), screen.yOffset, new SpriteColor(-1, -1, -1, 000), 1);
- /**
- * Rendering debug info
- */
- if (debug) {
- Font.render(fps + " Frames", screen, screen.xOffset, screen.yOffset + 16, new SpriteColor(-1, -1, -1, 000), 1);
- Font.render(updates + " Ticks", screen, screen.xOffset, screen.yOffset + 24, new SpriteColor(-1, -1, -1, 000), 1);
- Font.render("Debug Mode", screen, screen.xOffset, screen.yOffset + 32, new SpriteColor(-1, -1, -1, 000), 1);
- }
- if (player.isHurting()) {
- Hud.render(screen, 0, 0, 1, new SpriteColor(-1, 555, 222, -1), 20);
- } else {
- Hud.render(screen, 0, 0, 1, new SpriteColor(-1, 111, 222, -1), 20);
- }
- Hud.render(screen, 0, 0, 0, new SpriteColor(-1, 200, 300, 433), player.getHealth());
- Hud.render(screen, 0, 8, 3, new SpriteColor(-1, 111, 222, -1), 20);
- Hud.render(screen, 0, 8, 2, new SpriteColor(-1, 210, 300, 444), player.hunger);
- }
- /**
- * if the player dies
- */
- if (player.isRemoved()) {
- gui.add(new GuiGameOver());
- JukeBox.stopAllSounds();
- if (input.enter.isPressed()) {
- player.setMaxHealth(20);
- player.setHealth(20);
- player.hunger = 20;
- player.x = player.y = 160;
- score = 0;
- stop();
- }
- }
- gui.add(new GuiHotbar());
- gui.render(screen);
- for (int y = 0; y < screen.height; y++) {
- for (int x = 0; x < screen.width; x++) {
- int colorCode = screen.pixels[x + y * screen.width];
- if (colorCode < 255)
- pixels[x + y * WIDTH] = colors[colorCode];
- }
- }
- g.drawImage(image, 0, 0, WIDTH * SCALE, HEIGHT * SCALE, null);
- g.setColor(Color.WHITE);
- gui.renderGuiOverlays(screen, g);
- g.dispose();
- bs.show();
- }
- public static Configuration getConfig() {
- return config;
- }
- public static void main(String[] args) {
- try {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- } catch (Exception e) {
- e.printStackTrace();
- }
- config.loadSettings();
- DefaultCursor = toolkit.createCustomCursor(LoadingUtils.loadImage("/cursor.png"), new Point(0, 0), "Cursor");
- TyCursor = toolkit.createCustomCursor(LoadingUtils.loadImage("/tyCursor.png"), new Point(0, 0), "TyCursor");
- getLauncherInstance().start();
- }
- }
Add Comment
Please, Sign In to add comment