com.mygdx.game.HideAndSeek.HideAndSeekScreen.java Source code

Java tutorial

Introduction

Here is the source code for com.mygdx.game.HideAndSeek.HideAndSeekScreen.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mygdx.game.HideAndSeek;

import com.mygdx.game.HideAndSeek.Entity;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Circle;
import com.mygdx.game.AI_Bundle;
import java.util.ArrayList;

/**
 *
 * @author Mat
 */
public class HideAndSeekScreen implements Screen {

    AI_Bundle has;
    OrthographicCamera camera;

    public ShapeRenderer shapeRenderer;
    public ArrayList<Hider> hiders;
    public Seeker seeker;

    private static final boolean FOG_OF_WAR = true;

    public HideAndSeekScreen(AI_Bundle has) {
        this.has = has;
        this.camera = new OrthographicCamera();
        this.shapeRenderer = new ShapeRenderer();
        camera.setToOrtho(false, has.WIDTH, has.HEIGHT);
        hiders = new ArrayList<Hider>();
        hiders.add(new Hider(has.WIDTH / 4, has.HEIGHT / 4));
        seeker = new Seeker(has.WIDTH / 2, has.HEIGHT / 2);

    }

    @Override
    public void show() {
    }

    @Override
    public void render(float delta) {
        // clear the screen with a dark blue color. The
        // arguments to glClearColor are the red, green
        // blue and alpha component in the range [0,1]
        // of the color to be used to clear the screen.
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        // tell the camera to update its matrices.
        camera.update();

        // tell the SpriteBatch to render in the
        // coordinate system specified by the camera.
        has.batch.setProjectionMatrix(camera.combined);

        // begin a new batch and draw the bucket and
        // all drops
        if (FOG_OF_WAR) {
            shapeRenderer.setColor(Color.WHITE);
            shapeRenderer.begin(ShapeType.Filled);
            shapeRenderer.circle(seeker.x + seeker.imageWidth / 2, seeker.y + seeker.imageHeight / 2,
                    seeker.SIGHT_RANGE);
            shapeRenderer.end();
        }

        has.batch.begin();
        for (Entity e : hiders) {
            double distance = Math.sqrt(Math.pow(e.x + e.imageWidth / 2 - seeker.x - seeker.imageWidth / 2, 2)
                    + Math.pow(e.y + e.imageHeight / 2 - seeker.y - seeker.imageHeight / 2, 2));
            if (distance + Math.sqrt(e.imageWidth / 2) < seeker.SIGHT_RANGE) {
                has.batch.draw(e.image, e.x, e.y);
            }
        }
        has.batch.draw(seeker.image, seeker.x, seeker.y);
        has.batch.end();

        seeker.act(delta, has);
    }

    @Override
    public void resize(int i, int i1) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void hide() {
    }

    @Override
    public void dispose() {
        if (hiders.get(0) != null) {
            hiders.get(0).image.dispose();
        }
        seeker.image.dispose();
        shapeRenderer.dispose();

    }

}