com.bombinggames.clonkrageremade.ClonkRage.java Source code

Java tutorial

Introduction

Here is the source code for com.bombinggames.clonkrageremade.ClonkRage.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.bombinggames.clonkrageremade;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import java.util.ArrayList;

/**
 *
 * @author Benedikt Vogler
 */
public class ClonkRage implements ApplicationListener {

    static Pixmap getForeGround() {
        return foreGround;
    }

    public static ArrayList<Flint> getEntities() {
        return entities;
    }

    private static Pixmap foreGround;
    private static Pixmap backGround;
    private View view;
    private DevTools devtools;
    private static ArrayList<Flint> entities = new ArrayList<>();

    public ClonkRage() {

    }

    @Override
    public void create() {
        view = new View(this);
        devtools = new DevTools(0, 0);
        //koordinatensystem nach unten zeigend
        foreGround = new Pixmap(1920, 1080, Pixmap.Format.RGBA8888);
        //foreGround.setColor(new Color(0.4f, 0.5f, 0.9f, 1));
        //foreGround.fill();
        backGround = new Pixmap(1920, 1080, Pixmap.Format.RGBA8888);
        backGround.setColor(new Color(0.4f, 0.5f, 0.9f, 1));
        backGround.fill();
        int surfaceheight = 300;
        int amplitude = 50;
        float width = (float) (Math.PI / 300f);
        float colornoise = 0.3f;
        int curvatureHeight = 30;
        for (int x = 0; x < foreGround.getWidth(); x++) {
            //y position of surface
            int surface = (int) (Math.sin(width * x) * amplitude + surfaceheight);
            for (int y = 0; y < surface; y++) {
                float topCurvature = 0;//brightness of added top curvature
                if (y > surface - curvatureHeight) {//is in area of curvature
                    topCurvature = (1 - (y - surface) / (float) curvatureHeight) * 0.5f;
                }

                int color = ((int) ((topCurvature + 0.4f * (1 - colornoise / 2 + colornoise * Math.random()))
                        * 255) << 24)//R
                        | ((int) ((topCurvature + 0.3f * (1 - colornoise / 2 + colornoise * Math.random()))
                                * 255) << 16)//G
                        | ((int) ((topCurvature + 0.2f * (1 - colornoise / 2 + colornoise * Math.random()))
                                * 255) << 8)//B
                        | 1 * 255;//A
                if (x < 500) {
                    foreGround.drawPixel(x, y, color);
                }
                backGround.drawPixel(x, y,
                        ((int) ((0.2f * (1 - colornoise / 2 + colornoise * Math.random())) * 255) << 24)//R
                                | ((int) ((0.1f * (1 - colornoise / 2 + colornoise * Math.random())) * 255) << 16)//G
                                | ((int) ((0.1f * (1 - colornoise / 2 + colornoise * Math.random())) * 255) << 8)//B
                                | 1 * 255//A
                );
            }
        }
        spawnEnts();

    }

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void render() {
        Gdx.graphics.getDeltaTime();
        if (Gdx.input.isKeyPressed(Input.Keys.ANY_KEY)) {
            spawnEnts();
        }
        for (Flint entity : entities) {
            entity.update(Gdx.graphics.getDeltaTime());
        }
        entities.removeIf((t) -> t.shouldBeDisposed());
        devtools.update();
        view.render();
        devtools.render(view);
    }

    public void spawnEnts() {
        for (int i = 0; i < 100; i++) {
            entities.add(new Flint((int) (Math.random() * 1000), (int) (500 + Math.random() * 500)));
        }
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
        view.dispose();
    }

    Pixmap getBG() {
        return backGround;
    }

    Pixmap getFG() {
        return foreGround;
    }

}