public class MyGdxGame implements ApplicationListener
{
long lastTimeCounted;
float sinceChange;
float frameRate;
BitmapFont font;
GUI gui;
boolean[] pressed;
OrthographicCamera cam;
float camW;
float camH;
Texture texture;
ShapeRenderer shape;
static SpriteBatch batch;
ArrayList<Rectangle> blocks;
ArrayList<Gameobject> gameobject;
ArrayList<Gameobject> destroy;
static boolean delete;
static Vector2 playerSpd;
static float playerTheta;
Charakter player;
Pixmap playerPix;
static Texture playerTex;
static Animation playerAni;
Texture playerLegs1Tex;
Texture playerLegs2Tex;
Texture playerLegs3Tex;
Texture playerLegs4Tex;
static Texture bulletTex;
static Texture mBlue1Tex;
static Texture mBlue2Tex;
static Animation mGreenAni;
Texture mGreen1Tex;
Texture mGreen2Tex;
Texture mGreen3Tex;
Texture mGreen4Tex;
static Animation mYellowAni;
Texture mYellow1Tex;
Texture mYellow2Tex;
Texture mYellow3Tex;
Texture mYellow4Tex;
int terrainW;
int terrainH;
int S;
LevelLoader terrain;
@Override
public void create()
{
FrameRate();
cam = new OrthographicCamera();
camConfig();
gui = new GUI();
gui.Controller();
pressed = new boolean[20];
for(int i=0; i<20; i++) pressed[i] = false;
shape = new ShapeRenderer();
batch = new SpriteBatch();
terrain = new LevelLoader();
S = 32;
terrain.create(0);
terrainW = terrain.map.getWidth();
terrainH = terrain.map.getHeight();
blocks = new ArrayList();
gameobject = new ArrayList();
destroy = new ArrayList();
delete = false;
playerSpd = new Vector2(0,0);
playerTheta = 0;
player = new Charakter();
playerPix = new Pixmap(Gdx.files.internal("PNG/player1.png"));
playerTex = new Texture(playerPix);
playerLegs1Tex = new Texture(Gdx.files.internal("PNG/legs1.png"));
playerLegs2Tex = new Texture(Gdx.files.internal("PNG/legs2.png"));
playerLegs3Tex = new Texture(Gdx.files.internal("PNG/legs3.png"));
playerLegs4Tex = new Texture(Gdx.files.internal("PNG/legs4.png"));
TextureRegion t1 = new TextureRegion(playerLegs1Tex);
TextureRegion t2 = new TextureRegion(playerLegs2Tex);
TextureRegion t3 = new TextureRegion(playerLegs3Tex);
TextureRegion t4 = new TextureRegion(playerLegs4Tex);
TextureRegion[] t = {t1, t2, t3, t4};
playerAni = new Animation(0.1f, t);
playerAni.setPlayMode(Animation.PlayMode.LOOP_PINGPONG);
player.create(new Vector2(terrain.plStartX,terrain.plStartY));
gameobject.add(player);
bulletTex = new Texture(Gdx.files.internal("PNG/bullet1.png"));
mBlue1Tex = new Texture(Gdx.files.internal("PNG/Monsters/mBlue1.png"));
mBlue2Tex = new Texture(Gdx.files.internal("PNG/Monsters/mBlue2.png"));
mGreen1Tex = new Texture(Gdx.files.internal("PNG/Monsters/mGreen1.png"));
mGreen2Tex = new Texture(Gdx.files.internal("PNG/Monsters/mGreen2.png"));
mGreen3Tex = new Texture(Gdx.files.internal("PNG/Monsters/mGreen3.png"));
mGreen4Tex = new Texture(Gdx.files.internal("PNG/Monsters/mGreen4.png"));
TextureRegion m1 = new TextureRegion(mGreen1Tex);
TextureRegion m2 = new TextureRegion(mGreen2Tex);
TextureRegion m3 = new TextureRegion(mGreen3Tex);
TextureRegion m4 = new TextureRegion(mGreen4Tex);
TextureRegion[] m = {m1, m2, m3, m4};
mGreenAni = new Animation(0.1f, m);
mGreenAni.setPlayMode(Animation.PlayMode.LOOP_PINGPONG);
mYellow1Tex = new Texture(Gdx.files.internal("PNG/Monsters/mYellow1.png"));
mYellow2Tex = new Texture(Gdx.files.internal("PNG/Monsters/mYellow2.png"));
mYellow3Tex = new Texture(Gdx.files.internal("PNG/Monsters/mYellow3.png"));
mYellow4Tex = new Texture(Gdx.files.internal("PNG/Monsters/mYellow4.png"));
TextureRegion mm1 = new TextureRegion(mYellow1Tex);
TextureRegion mm2 = new TextureRegion(mYellow2Tex);
TextureRegion mm3 = new TextureRegion(mYellow3Tex);
TextureRegion mm4 = new TextureRegion(mYellow4Tex);
TextureRegion[] mm = {mm1, mm2, mm3, mm4};
mYellowAni = new Animation(0.1f, mm);
mYellowAni.setPlayMode(Animation.PlayMode.LOOP_PINGPONG);
}
@Override
public void render()
{
Vector2 plPos = player.getPos();
float viewX = plPos.x;// - camW/2;
float viewY = plPos.y;// - camH/2;
Vector2 vtmp = new Vector2(cam.position.x, cam.position.y);
vtmp.lerp(new Vector2(viewX, viewY), 0.3f);
cam.position.x = vtmp.x;
cam.position.y = vtmp.y;
cam.update();
Gdx.gl.glClearColor(0.6f, 0.5f, 0.3f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//////// blocks drawing ////////
batch.setProjectionMatrix(cam.combined);
batch.begin();
terrain.draw(batch, (int)((viewX-camW/2)/S-4), (int)((viewY-camH/2)/S-4), (int)camW/S+8,(int)camH/S+8);
//Charas
for (Gameobject obj : gameobject) {
obj.draw();
}
batch.end();
//FPS
update();
gui.camGui.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
batch.setProjectionMatrix(gui.camGui.combined);
batch.begin();
font.draw(batch, (int)frameRate + " fps", 5, Gdx.graphics.getHeight() - 5);
batch.end();
//Buttons
pressed = gui.draw();
///////// Collision Checking ////////
blocks.clear();
int ii = (int)(viewX/S)-4;
int ii2 = ii + (int)(camW/S)+8;
int jj = (int)(viewY/S)-4;
int jj2 = jj + (int)(camH/S)+8;
ii = MathUtils.clamp(ii, 0, terrain.W);
ii2 = MathUtils.clamp(ii2, 0, terrain.W);
jj = MathUtils.clamp(jj, 0, terrain.H);
jj2 = MathUtils.clamp(jj2, 0, terrain.H);
for(int i=ii; i<ii2; i++) {
for(int j=jj; j<jj2; j++) {
if(terrain.block[i][j] > 0) {
blocks.add(new Rectangle(i*S,j*S, S,S));
}
}
}
////////////////////////////
////// So lassen !!!!! /////
////////////////////////////
//Player Control
Vector2 plVel = player.getVel();
plVel.add(playerSpd);
player.setVel(plVel); //Methode fur MyGdxGame / pos etc. private
playerSpd = new Vector2(0,0);
if(pressed[1] == true) {
Bullet b = new Bullet();
Vector2 v = player.getPos();
v.add(new Vector2(200,100));
b.create(v);
v = new Vector2(300,0);
b.setVel(v);
gameobject.add(b);
}
/*if(pressed[0]==true) {
Vector2 speed = new Vector2(-c.gravDir.y * c.walkSpd * Gdx.graphics.getDeltaTime(),
-c.gravDir.x * c.walkSpd * Gdx.graphics.getDeltaTime());
c.Vel.x -= speed.x;
c.Vel.x = MathUtils.clamp(c.Vel.x, -c.maxSpd, c.maxSpd);
c.Vel.y -= speed.y;
c.Vel.y = MathUtils.clamp(c.Vel.y, -c.maxSpd, c.maxSpd);
int xx = (int)c.Pos.x / S;
int yy = (int)c.Pos.y / S;
/*if(terrain.block[xx][yy] > 3 && terrain.block[xx-(int)c.gravDir.x][yy-(int)c.gravDir.y] < 4) {
c.Pos.x -= c.gravDir.x * (S+2);
c.Pos.y -= c.gravDir.y * (S+2);
}*/
/*}
if(pressed[0]==true) {
Vector2 speed = new Vector2(-c.gravDir.y * c.walkSpd * Gdx.graphics.getDeltaTime(),
-c.gravDir.x * c.walkSpd * Gdx.graphics.getDeltaTime());
c.Vel.x += speed.x;
c.Vel.x = MathUtils.clamp(c.Vel.x, -c.maxSpd, c.maxSpd);
c.Vel.y += speed.y;
c.Vel.y = MathUtils.clamp(c.Vel.y, -c.maxSpd, c.maxSpd);
int xx = (int)c.Pos.x / S;
int yy = (int)c.Pos.y / S;
/*if(terrain.block[xx][yy] > 3 && terrain.block[xx-(int)c.gravDir.x][yy-(int)c.gravDir.y] < 4) {
c.Pos.x -= c.gravDir.x * (S+2);
c.Pos.y -= c.gravDir.y * (S+2);
}*/
/*}
if(pressed[0]==true) {
c.Vel.sub(c.gravDir.x * c.jumpSpd * Gdx.graphics.getDeltaTime(),
c.gravDir.y * c.jumpSpd * Gdx.graphics.getDeltaTime());
}*/
//////////////////////////////////
for (Gameobject obj : gameobject) {
for (Rectangle b : blocks) {
obj.collisionCheck(b);
}
obj.action();
if(delete == true) destroy.add(obj);
delete = false;
}
for (Gameobject obj : destroy) {
gameobject.remove(obj);
}
}
@Override
public void dispose()
{
}
@Override
public void resize(int width, int height)
{
camConfig();
}
@Override
public void pause()
{
}
@Override
public void resume()
{
}
public void update() {
long delta = TimeUtils.timeSinceMillis(lastTimeCounted);
lastTimeCounted = TimeUtils.millis();
sinceChange += delta;
if(sinceChange >= 1000) {
sinceChange = 0;
frameRate = Gdx.graphics.getFramesPerSecond();
}
}
public void FrameRate() {
lastTimeCounted = TimeUtils.millis();
sinceChange = 0;
frameRate = Gdx.graphics.getFramesPerSecond();
font = new BitmapFont();
font.scale(2);
batch = new SpriteBatch();
cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
public void camConfig() {
float size = 1000;
if (Gdx.graphics.getHeight() < Gdx.graphics.getWidth()) {
camH = size * Gdx.graphics.getHeight() / Gdx.graphics.getWidth();
camW = size;
cam.setToOrtho(false, camW, camH);
}
else {
camW = size * Gdx.graphics.getWidth() / Gdx.graphics.getHeight();
camH = size;
cam.setToOrtho(false, camW, camH);
}
}
}