package com.gcstudios.main;
import java.util.List;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.JFrame;
import com.gcstudios.entities.Entity;
import com.gcstudios.entities.Player;
import com.gcstudios.graficos.Spritesheet;
public class Game extends Canvas implements Runnable,KeyListener{
private static final long serialVersionUID = 1L;
public static JFrame frame;
private boolean isRunning = true;
public static int WIDTH = 240;
public static int HEIGHT = 160;
public static int SCALE = 3;
public BufferedImage layer = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
public List<Entity> entities;
public static Spritesheet spritesheet;
public static int widht;
public static int height;
private Player player;
public Game() {
addKeyListener(this);
this.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
this.addKeyListener(this);
entities = new ArrayList<Entity>();
spritesheet = new Spritesheet("/spritesheet.png");
player = new Player(0, 0, 16, 16, spritesheet.getsprite(32, 0, 16, 16));
entities.add(player);
}
private void addKeyListener(Game game) {
// TODO Auto-generated method stub
}
public static void main(String[] args){
Game game = new Game ();
JFrame frame = new JFrame("Game");
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(game);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
new Thread(game).start();
}
public void tick() {
for(int i = 0; i < entities.size(); i++) {
Entity e = (Entity) entities.get(i);
e.tick();
}
}
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = layer.getGraphics();
g.setColor(new Color(0,255,0) );
g.fillRect(0, 0, WIDTH, HEIGHT);
for(int i = 0; i < entities.size(); i++) {
Entity e = (Entity) entities.get(i);
e.render(g);
}
g = bs.getDrawGraphics();
g.drawImage(layer, 0, 0, WIDTH*SCALE, HEIGHT*SCALE,null);
bs.show();
}
@Override
public void run() {
while(true) {
tick();
render();
try {
Thread.sleep(1000/60);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()== KeyEvent.VK_RIGHT ||
e.getKeyCode() == KeyEvent.VK_D){
player.right=true;
}else if(e.getKeyCode()== KeyEvent.VK_LEFT||
e.getKeyCode() == KeyEvent.VK_A){
player.left=true;
}
if(e.getKeyCode()== KeyEvent.VK_UP ||
e.getKeyCode() == KeyEvent.VK_W){
player.up=true;
System.out.println("Cima");
}else if(e.getKeyCode()== KeyEvent.VK_DOWN ||
e.getKeyCode() == KeyEvent.VK_S){
player.down=true;
}
{
}
{
}
}
public void keyReleased(KeyEvent e) {
if(e.getKeyCode()== KeyEvent.VK_RIGHT ||
e.getKeyCode() == KeyEvent.VK_D){
player.right=false;
System.out.println("Direita");
}else if(e.getKeyCode()== KeyEvent.VK_LEFT||
e.getKeyCode() == KeyEvent.VK_A){
player.left=false;
System.out.println("Esquerda");
}
if(e.getKeyCode()== KeyEvent.VK_UP ||
e.getKeyCode() == KeyEvent.VK_W){
player.up=false;
System.out.println("Cima");
}else if(e.getKeyCode()== KeyEvent.VK_DOWN ||
e.getKeyCode() == KeyEvent.VK_S){
player.down=false;
System.out.println("Baixo");
}
{
}
}
@Override
public void keyTyped(KeyEvent arg0) {
}
public boolean isRunning() {
return isRunning;
}
public void setRunning(boolean isRunning) {
this.isRunning = isRunning;
}
}