Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.loyal.main;
- import java.awt.Canvas;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.image.BufferStrategy;
- import java.awt.image.BufferedImage;
- import javax.swing.JFrame;
- public class MainComponent extends Canvas implements Runnable {
- private static final long serialVersionUID = 1L;
- public static final int WIDTH = 1000,
- HEIGHT = 563;
- private boolean running = false;
- private Camera camera;
- private Thread thread;
- private BufferedImage levelimg;
- private Game game;
- private Input input;
- public MainComponent() {
- input = new Input();
- this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
- this.setMaximumSize(new Dimension(WIDTH, HEIGHT));
- this.setMinimumSize(new Dimension(WIDTH, HEIGHT));
- this.addKeyListener(input);
- game = new Game(this);
- levelimg = new ImageLoader().load("/image.png");
- camera = new Camera(0, 0);
- JFrame frame = new JFrame(":D");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setResizable(false);
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- frame.add(this);
- frame.pack();
- loadLevel(levelimg);
- }
- public synchronized void start() {
- if(running)
- return;
- running = true;
- thread = new Thread(this);
- thread.run();
- }
- public synchronized void stop() {
- if(!running)
- return;
- running = false;
- try {
- thread.join();
- } catch(InterruptedException e) {
- e.printStackTrace();
- }
- }
- public void run() {
- this.requestFocus();
- long last = System.nanoTime();
- long now;
- long timer = System.currentTimeMillis();
- int frames = 0;
- int ticks = 0;
- double delta = 0;
- double ns = 1000000000 / 60;
- boolean render = true;
- while(running) {
- now = System.nanoTime();
- delta += (now - last) / ns;
- last = now;
- while(delta >= 1) {
- tick();
- ticks++;
- render = true;
- delta--;
- }
- try {
- thread.sleep(2);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- if(render) {
- render();
- frames++;
- }
- if(System.currentTimeMillis() - timer >= 1000) {
- System.out.println(ticks + " " + frames);
- frames = 0;
- ticks = 0;
- timer += 1000;
- }
- }
- stop();
- }
- public void tick() {
- game.tick();
- for(int i = 0; i < game.entities.size(); i++) {
- if(game.entities.get(i).getID() == ID.Player) {
- camera.setPos(game.entities.get(i));
- }
- }
- }
- public void render() {
- BufferStrategy bs = this.getBufferStrategy();
- if(bs == null) {
- this.createBufferStrategy(3);
- return;
- }
- Graphics g = bs.getDrawGraphics();
- Graphics2D g2d = (Graphics2D) g;
- g2d.translate(-camera.getX(), -camera.getY());
- g.clearRect((int)camera.getX(), (int)camera.getY(), WIDTH, HEIGHT);
- g.setColor(Color.RED);
- g.fillRect((int)camera.getX(), (int)camera.getY(), WIDTH, HEIGHT);
- game.render(g);
- g2d.translate(camera.getX(), camera.getY());
- g.dispose();
- bs.show();
- }
- private void loadLevel(BufferedImage image) {
- int W = image.getWidth();
- int H = image.getHeight();
- for(int y = 0; y < H; y++) {
- for(int x = 0; x < W; x++) {
- int pixel = image.getRGB(x, y);
- int red = (pixel >> 16) & 0xff;
- int green = (pixel >> 8) & 0xff;
- int blue = (pixel) & 0xff;
- if(red == 255) {
- Game.entities.add(new Block(x*32, y*32));
- }
- if(blue == 255) {
- Game.entities.add(new Player(x*32, y*32));
- }
- }
- }
- }
- public static void main(String[] args) {
- new MainComponent().start();;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement