Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Main Class
- public class Game extends Canvas implements Runnable {
- public static final int HEIGHT = 300, WIDTH = HEIGHT * 16 / 9;
- public static final int SCALE = 3;
- public int tickCount = 0;
- public int healthPosX = 50, healthPosY = 678;
- public int staminaPosX = 95, staminaPosY = 725;
- public BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
- public int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
- private JFrame frame;
- private Screen screen;
- private Level level;
- private Player player;
- private Mob mob;
- private Hud hud;
- public static InputHandler input;
- public boolean running = false;
- public String NAME = "Digger";
- public Game() {
- setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
- setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
- setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
- frame = new JFrame(NAME);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLayout(new BorderLayout());
- frame.add(this, BorderLayout.CENTER);
- frame.pack();
- frame.setResizable(true);
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- input = new InputHandler(this);
- screen = new Screen(WIDTH, HEIGHT);
- level = new Level("/Level.png");
- player = new Player(level, 50, 50, input);
- hud = new Hud(level, 15, 16 * 15);
- level.addEntity(player);
- }
- public static void main(String[] a) {
- new Game().start();
- }
- public void init() {
- }
- private void start() {
- running = true;
- new Thread(this).start();
- }
- private void stop() {
- running = false;
- }
- @Override
- public void run() {
- long lastTime = System.nanoTime();
- double nsPerTick = 1000000000D / 60D;
- int frames = 0;
- int ticks = 0;
- long lastTimer = System.currentTimeMillis();
- double delta = 0;
- init();
- requestFocus();
- while (running) {
- long now = System.nanoTime();
- delta += (now - lastTime) / nsPerTick;
- lastTime = now;
- boolean shouldRender = true;
- while (delta >= 1) {
- ticks++;
- tick();
- delta -= 1;
- shouldRender = true;
- }
- try {
- Thread.sleep(2);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- if (shouldRender) {
- frames++;
- render();
- }
- if (System.currentTimeMillis() - lastTimer > 1000) {
- lastTimer += 1000;
- System.out.println(ticks + " ticks, " + frames + " frames");
- frames = 0;
- ticks = 0;
- shouldRender = true;
- }
- }
- }
- private void tick() {
- tickCount++;
- level.tick();
- for (int i = 0; i < pixels.length; i++) {
- pixels[i] = i + tickCount;
- }
- }
- private void render() {
- BufferStrategy bs = getBufferStrategy();
- if (bs == null) {
- createBufferStrategy(3);
- return;
- }
- int xOffset = player.x - screen.width / 2;
- int yOffset = player.y - screen.height / 2;
- level.render(xOffset, yOffset, screen);
- level.renderEntities(screen);
- player.render(screen);
- hud.render(screen);
- for (int i = 0; i < pixels.length; i++) {
- pixels[i] = screen.pixel[i];
- }
- Graphics g = bs.getDrawGraphics();
- g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
- g.setColor(Color.RED);
- //health bar
- g.fillRect(healthPosX, (int) (healthPosY + 132 * (1 - player.healthPercent)), 38, (int) (132 * player.healthPercent));
- g.setColor(Color.YELLOW);
- //stamina bar
- g.fillRect(staminaPosX, (int) (staminaPosY + 85 * (1 - player.staminaPercent)), 38, (int) (85 * player.staminaPercent));
- g.dispose();
- bs.show();
- }
- }
- // Mob Class
- package display.entities;
- import level.Level;
- import level.tiles.Tile;
- public abstract class Mob extends Entity {
- protected String name;
- protected int speed;
- protected Player player;
- protected int numSteps = 0;
- public boolean isDead = false;
- protected int movingDir = 0;
- protected boolean isMoving;
- public Mob(Level level, String name, int x, int y, int speed) {
- super(level);
- this.name = name;
- this.speed = speed;
- this.x = x;
- this.y = y;
- }
- public void move(int xa, int ya) {
- if (xa != 0 && ya != 0) {
- move(xa, 0);
- move(0, ya);
- numSteps--;
- return;
- }
- numSteps++;
- if (!hasCollided(xa, ya)) {
- if (xa > 0) {
- movingDir = 1;
- }
- if (xa < 0) {
- movingDir = 2;
- }
- x += xa * speed;
- y += ya * speed;
- }
- else {
- xa = 0;
- ya = 0;
- }
- }
- public abstract boolean hasCollided(int xa, int ya);
- public abstract boolean hitHarmfulTile(int xa, int ya);
- protected boolean isSolidTile(int xa, int ya, int x, int y) {
- if (level == null) {
- return false;
- }
- Tile lastTile = level.getTile((this.x + x) >> 4, (this.y + y) >> 4);
- Tile newTile = level.getTile((this.x + x + xa) >> 4, (this.y + y + ya) >> 4);
- if (!lastTile.equals(newTile) && newTile.isSolid()) {
- return true;
- }
- return false;
- }
- protected boolean isHarmfulTile(int xa, int ya, int x, int y) {
- if (level == null) {
- return false;
- }
- Tile lastTile = level.getTile((this.x + x) >> 4, (this.y + y) >> 4);
- Tile newTile = level.getTile((this.x + x + xa) >> 4, (this.y + y + ya) >> 4);
- if (!lastTile.equals(newTile) && newTile.isHarmful()) {
- return true;
- }
- return false;
- }
- public String getName() {
- return name;
- }
- }
- // Player Class
- package display.entities;
- import gfx.Screen;
- import gfx.Sprite;
- import level.Level;
- import level.tiles.Tile;
- import display.InputHandler;
- public class Player extends Mob {
- private Screen screen;
- private InputHandler input;
- private boolean moving = false;
- public int jumpTimer = 0;
- public double health = 100;
- public int healing = 0;
- public double healthPercent;
- public int staminaDegen = 0;
- public double stamina = 50;
- public double staminaPercent;
- public int updates = 0;
- private Tile tile;
- private boolean jumping = false;
- public Player(Level level, int x, int y, InputHandler input) {
- super(level, "Player", x, y, 1);
- this.input = input;
- }
- public boolean hasCollided(int xa, int ya) {
- int xMin = 2;
- int xMax = 19;
- int yMin = 0;
- int yMax = 29;
- for (int x = xMin; x < xMax; x++) {
- if (isSolidTile(xa, ya, x, yMin)) {
- return true;
- }
- }
- for (int x = xMin; x < xMax; x++) {
- if (isSolidTile(xa, ya, x, yMax)) {
- return true;
- }
- }
- for (int y = yMin; y < yMax; y++) {
- if (isSolidTile(xa, ya, xMin, y)) {
- return true;
- }
- }
- for (int y = yMin; y < yMax; y++) {
- if (isSolidTile(xa, ya, xMax, y)) {
- return true;
- }
- }
- return false;
- }
- public boolean hitHarmfulTile(int xa, int ya) {
- int xMin = 2;
- int xMax = 19;
- int yMin = 0;
- int yMax = 29;
- for (int x = xMin; x < xMax; x++) {
- if (isHarmfulTile(xa, ya, x, yMin)) {
- return true;
- }
- }
- for (int x = xMin; x < xMax; x++) {
- if (isHarmfulTile(xa, ya, x, yMax)) {
- return true;
- }
- }
- for (int y = yMin; y < yMax; y++) {
- if (isHarmfulTile(xa, ya, xMin, y)) {
- return true;
- }
- }
- for (int y = yMin; y < yMax; y++) {
- if (isHarmfulTile(xa, ya, xMax, y)) {
- return true;
- }
- }
- return false;
- }
- public boolean die(int xa, int ya) {
- if (hitHarmfulTile(xa, ya)) {
- return true;
- } else {
- return false;
- }
- }
- @Override
- public void tick() {
- moving = false;
- updates++;
- healthPercent = health / 100;
- staminaPercent = stamina / 50;
- healing++;
- if (healing > 50) {
- health += 5;
- stamina += 5;
- healing = 0;
- }
- if (health < 0 || health > 100) {
- health = 100;
- }
- if (stamina < 0 || stamina > 50) {
- stamina = 50;
- }
- if (updates > 1000) {
- updates = 0;
- }
- int xa = 0;
- int ya = 0;
- ya = ya + 3;
- if (input.left.isPressed()) {
- if (input.sprint.isPressed() && stamina > 0) {
- xa -= 4;
- staminaDegen++;
- if(staminaDegen >= 2){
- stamina--;
- staminaDegen = 0;
- }
- moving = true;
- } else {
- xa -= 2;
- moving = true;
- }
- }
- if (input.right.isPressed()) {
- if (input.sprint.isPressed() && stamina > 0) {
- xa += 4;
- staminaDegen++;
- if(staminaDegen >= 2){
- stamina--;
- staminaDegen = 0;
- }
- moving = true;
- } else {
- xa += 2;
- moving = true;
- }
- }
- if (input.jump.isPressed() && jumpTimer < 15) {
- ya = ya - 6;
- moving = true;
- jumpTimer++;
- jumping = true;
- }
- if (xa != 0 || ya != 0) {
- move(xa, ya);
- isMoving = true;
- } else {
- isMoving = false;
- }
- if (hasCollided(xa, ya)) {
- jumpTimer = 0;
- jumping = false;
- }
- if (die(xa, ya)) {
- health -= 1;
- }
- }
- @Override
- public void render(Screen screen) {
- Sprite sprite = Sprite.playerIdle;
- if (updates % 40 > 20) {
- sprite = sprite.playerIdle;
- }
- if (updates % 40 > 20) {
- sprite = sprite.playerIdle2;
- }
- if (!die(x, y)) {
- screen.renderPlayer(x, y, sprite);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement