Advertisement
freshyfreshfresh

Digger

Apr 2nd, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.01 KB | None | 0 0
  1. //                  Main Class
  2.  
  3. public class Game extends Canvas implements Runnable {
  4.  
  5.     public static final int HEIGHT = 300, WIDTH = HEIGHT * 16 / 9;
  6.     public static final int SCALE = 3;
  7.     public int tickCount = 0;
  8.     public int healthPosX = 50, healthPosY = 678;
  9.     public int staminaPosX = 95, staminaPosY = 725;
  10.  
  11.     public BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
  12.     public int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
  13.  
  14.     private JFrame frame;
  15.     private Screen screen;
  16.     private Level level;
  17.     private Player player;
  18.     private Mob mob;
  19.     private Hud hud;
  20.  
  21.     public static InputHandler input;
  22.     public boolean running = false;
  23.     public String NAME = "Digger";
  24.  
  25.     public Game() {
  26.  
  27.         setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
  28.         setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
  29.         setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
  30.  
  31.         frame = new JFrame(NAME);
  32.  
  33.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  34.         frame.setLayout(new BorderLayout());
  35.         frame.add(this, BorderLayout.CENTER);
  36.         frame.pack();
  37.  
  38.         frame.setResizable(true);
  39.         frame.setLocationRelativeTo(null);
  40.         frame.setVisible(true);
  41.  
  42.         input = new InputHandler(this);
  43.  
  44.         screen = new Screen(WIDTH, HEIGHT);
  45.         level = new Level("/Level.png");
  46.  
  47.         player = new Player(level, 50, 50, input);
  48.  
  49.         hud = new Hud(level, 15, 16 * 15);
  50.         level.addEntity(player);
  51.  
  52.     }
  53.  
  54.     public static void main(String[] a) {
  55.         new Game().start();
  56.     }
  57.  
  58.     public void init() {
  59.  
  60.     }
  61.  
  62.     private void start() {
  63.         running = true;
  64.         new Thread(this).start();
  65.     }
  66.  
  67.     private void stop() {
  68.         running = false;
  69.     }
  70.  
  71.     @Override
  72.     public void run() {
  73.         long lastTime = System.nanoTime();
  74.         double nsPerTick = 1000000000D / 60D;
  75.  
  76.         int frames = 0;
  77.         int ticks = 0;
  78.  
  79.         long lastTimer = System.currentTimeMillis();
  80.         double delta = 0;
  81.  
  82.         init();
  83.         requestFocus();
  84.  
  85.         while (running) {
  86.             long now = System.nanoTime();
  87.             delta += (now - lastTime) / nsPerTick;
  88.             lastTime = now;
  89.             boolean shouldRender = true;
  90.  
  91.             while (delta >= 1) {
  92.                 ticks++;
  93.                 tick();
  94.                 delta -= 1;
  95.                 shouldRender = true;
  96.             }
  97.  
  98.             try {
  99.                 Thread.sleep(2);
  100.             } catch (InterruptedException e) {
  101.                 e.printStackTrace();
  102.             }
  103.  
  104.             if (shouldRender) {
  105.                 frames++;
  106.                 render();
  107.             }
  108.  
  109.             if (System.currentTimeMillis() - lastTimer > 1000) {
  110.                 lastTimer += 1000;
  111.                 System.out.println(ticks + " ticks, " + frames + " frames");
  112.                 frames = 0;
  113.                 ticks = 0;
  114.                 shouldRender = true;
  115.             }
  116.         }
  117.     }
  118.  
  119.     private void tick() {
  120.         tickCount++;
  121.         level.tick();
  122.  
  123.         for (int i = 0; i < pixels.length; i++) {
  124.             pixels[i] = i + tickCount;
  125.         }
  126.     }
  127.  
  128.     private void render() {
  129.         BufferStrategy bs = getBufferStrategy();
  130.         if (bs == null) {
  131.             createBufferStrategy(3);
  132.             return;
  133.         }
  134.  
  135.         int xOffset = player.x - screen.width / 2;
  136.         int yOffset = player.y - screen.height / 2;
  137.  
  138.         level.render(xOffset, yOffset, screen);
  139.         level.renderEntities(screen);
  140.  
  141.         player.render(screen);
  142.         hud.render(screen);
  143.  
  144.         for (int i = 0; i < pixels.length; i++) {
  145.             pixels[i] = screen.pixel[i];
  146.         }
  147.  
  148.         Graphics g = bs.getDrawGraphics();
  149.  
  150.         g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
  151.         g.setColor(Color.RED);
  152.         //health bar
  153.         g.fillRect(healthPosX, (int) (healthPosY + 132 * (1 - player.healthPercent)), 38, (int) (132 * player.healthPercent));
  154.         g.setColor(Color.YELLOW);
  155.         //stamina bar
  156.         g.fillRect(staminaPosX, (int) (staminaPosY + 85 * (1 - player.staminaPercent)), 38, (int) (85 * player.staminaPercent));
  157.         g.dispose();
  158.         bs.show();
  159.  
  160.     }
  161.  
  162. }
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172. //                       Mob Class
  173.  
  174.  
  175. package display.entities;
  176.  
  177. import level.Level;
  178. import level.tiles.Tile;
  179.  
  180. public abstract class Mob extends Entity {
  181.  
  182.     protected String name;
  183.     protected int speed;
  184.     protected Player player;
  185.     protected int numSteps = 0;
  186.     public boolean isDead = false;
  187.     protected int movingDir = 0;
  188.     protected boolean isMoving;
  189.  
  190.     public Mob(Level level, String name, int x, int y, int speed) {
  191.         super(level);
  192.         this.name = name;
  193.         this.speed = speed;
  194.         this.x = x;
  195.         this.y = y;
  196.     }
  197.  
  198.     public void move(int xa, int ya) {
  199.         if (xa != 0 && ya != 0) {
  200.             move(xa, 0);
  201.             move(0, ya);
  202.             numSteps--;
  203.             return;
  204.         }
  205.        
  206.         numSteps++;
  207.  
  208.         if (!hasCollided(xa, ya)) {
  209.             if (xa > 0) {
  210.                 movingDir = 1;
  211.             }
  212.             if (xa < 0) {
  213.                 movingDir = 2;
  214.             }
  215.             x += xa * speed;
  216.             y += ya * speed;
  217.         }
  218.  
  219.         else {
  220.             xa = 0;
  221.             ya = 0;
  222.         }
  223.  
  224.        
  225.  
  226.     }
  227.  
  228.    
  229.  
  230.     public abstract boolean hasCollided(int xa, int ya);
  231.  
  232.     public abstract boolean hitHarmfulTile(int xa, int ya);
  233.  
  234.     protected boolean isSolidTile(int xa, int ya, int x, int y) {
  235.         if (level == null) {
  236.             return false;
  237.         }
  238.  
  239.         Tile lastTile = level.getTile((this.x + x) >> 4, (this.y + y) >> 4);
  240.         Tile newTile = level.getTile((this.x + x + xa) >> 4, (this.y + y + ya) >> 4);
  241.         if (!lastTile.equals(newTile) && newTile.isSolid()) {
  242.             return true;
  243.         }
  244.         return false;
  245.     }
  246.  
  247.     protected boolean isHarmfulTile(int xa, int ya, int x, int y) {
  248.         if (level == null) {
  249.             return false;
  250.         }
  251.  
  252.         Tile lastTile = level.getTile((this.x + x) >> 4, (this.y + y) >> 4);
  253.         Tile newTile = level.getTile((this.x + x + xa) >> 4, (this.y + y + ya) >> 4);
  254.         if (!lastTile.equals(newTile) && newTile.isHarmful()) {
  255.             return true;
  256.         }
  257.         return false;
  258.     }
  259.  
  260.     public String getName() {
  261.         return name;
  262.     }
  263.  
  264. }
  265.  
  266.  
  267.  
  268. //                       Player Class
  269.  
  270.  
  271.  
  272. package display.entities;
  273.  
  274. import gfx.Screen;
  275. import gfx.Sprite;
  276. import level.Level;
  277. import level.tiles.Tile;
  278. import display.InputHandler;
  279.  
  280. public class Player extends Mob {
  281.  
  282.     private Screen screen;
  283.     private InputHandler input;
  284.     private boolean moving = false;
  285.     public int jumpTimer = 0;
  286.     public double health = 100;
  287.     public int healing = 0;
  288.     public double healthPercent;
  289.     public int staminaDegen = 0;
  290.     public double stamina = 50;
  291.     public double staminaPercent;
  292.     public int updates = 0;
  293.     private Tile tile;
  294.  
  295.     private boolean jumping = false;
  296.  
  297.     public Player(Level level, int x, int y, InputHandler input) {
  298.         super(level, "Player", x, y, 1);
  299.         this.input = input;
  300.     }
  301.  
  302.     public boolean hasCollided(int xa, int ya) {
  303.         int xMin = 2;
  304.         int xMax = 19;
  305.         int yMin = 0;
  306.         int yMax = 29;
  307.         for (int x = xMin; x < xMax; x++) {
  308.             if (isSolidTile(xa, ya, x, yMin)) {
  309.                 return true;
  310.  
  311.             }
  312.         }
  313.         for (int x = xMin; x < xMax; x++) {
  314.             if (isSolidTile(xa, ya, x, yMax)) {
  315.                 return true;
  316.  
  317.             }
  318.         }
  319.         for (int y = yMin; y < yMax; y++) {
  320.             if (isSolidTile(xa, ya, xMin, y)) {
  321.                 return true;
  322.             }
  323.         }
  324.         for (int y = yMin; y < yMax; y++) {
  325.             if (isSolidTile(xa, ya, xMax, y)) {
  326.                 return true;
  327.             }
  328.         }
  329.  
  330.         return false;
  331.     }
  332.  
  333.     public boolean hitHarmfulTile(int xa, int ya) {
  334.         int xMin = 2;
  335.         int xMax = 19;
  336.         int yMin = 0;
  337.         int yMax = 29;
  338.         for (int x = xMin; x < xMax; x++) {
  339.             if (isHarmfulTile(xa, ya, x, yMin)) {
  340.                 return true;
  341.  
  342.             }
  343.         }
  344.         for (int x = xMin; x < xMax; x++) {
  345.             if (isHarmfulTile(xa, ya, x, yMax)) {
  346.                 return true;
  347.  
  348.             }
  349.         }
  350.         for (int y = yMin; y < yMax; y++) {
  351.             if (isHarmfulTile(xa, ya, xMin, y)) {
  352.                 return true;
  353.             }
  354.         }
  355.         for (int y = yMin; y < yMax; y++) {
  356.             if (isHarmfulTile(xa, ya, xMax, y)) {
  357.                 return true;
  358.             }
  359.         }
  360.  
  361.         return false;
  362.     }
  363.  
  364.     public boolean die(int xa, int ya) {
  365.         if (hitHarmfulTile(xa, ya)) {
  366.             return true;
  367.         } else {
  368.             return false;
  369.         }
  370.     }
  371.  
  372.     @Override
  373.     public void tick() {
  374.         moving = false;
  375.         updates++;
  376.         healthPercent = health / 100;
  377.         staminaPercent = stamina / 50;
  378.         healing++;
  379.  
  380.         if (healing > 50) {
  381.             health += 5;
  382.             stamina += 5;
  383.             healing = 0;
  384.         }
  385.         if (health < 0 || health > 100) {
  386.             health = 100;
  387.         }
  388.         if (stamina < 0 || stamina > 50) {
  389.             stamina = 50;
  390.         }
  391.         if (updates > 1000) {
  392.             updates = 0;
  393.  
  394.         }
  395.         int xa = 0;
  396.         int ya = 0;
  397.  
  398.         ya = ya + 3;
  399.  
  400.         if (input.left.isPressed()) {
  401.             if (input.sprint.isPressed() && stamina > 0) {
  402.                 xa -= 4;
  403.                 staminaDegen++;
  404.                 if(staminaDegen >= 2){
  405.                     stamina--;
  406.                     staminaDegen = 0;
  407.                 }
  408.                 moving = true;
  409.             } else {
  410.                 xa -= 2;
  411.                 moving = true;
  412.             }
  413.         }
  414.         if (input.right.isPressed()) {
  415.             if (input.sprint.isPressed() && stamina > 0) {
  416.                 xa += 4;
  417.                 staminaDegen++;
  418.                 if(staminaDegen >= 2){
  419.                     stamina--;
  420.                     staminaDegen = 0;
  421.                 }
  422.  
  423.                 moving = true;
  424.             } else {
  425.                 xa += 2;
  426.                 moving = true;
  427.             }
  428.         }
  429.         if (input.jump.isPressed() && jumpTimer < 15) {
  430.             ya = ya - 6;
  431.             moving = true;
  432.             jumpTimer++;
  433.             jumping = true;
  434.         }
  435.  
  436.         if (xa != 0 || ya != 0) {
  437.             move(xa, ya);
  438.             isMoving = true;
  439.         } else {
  440.             isMoving = false;
  441.  
  442.         }
  443.  
  444.         if (hasCollided(xa, ya)) {
  445.             jumpTimer = 0;
  446.             jumping = false;
  447.         }
  448.         if (die(xa, ya)) {
  449.             health -= 1;
  450.         }
  451.  
  452.     }
  453.  
  454.     @Override
  455.     public void render(Screen screen) {
  456.         Sprite sprite = Sprite.playerIdle;
  457.         if (updates % 40 > 20) {
  458.             sprite = sprite.playerIdle;
  459.         }
  460.         if (updates % 40 > 20) {
  461.             sprite = sprite.playerIdle2;
  462.         }
  463.  
  464.         if (!die(x, y)) {
  465.             screen.renderPlayer(x, y, sprite);
  466.         }
  467.     }
  468. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement