com.tnf.ptm.gfx.particle.PartMan.java Source code

Java tutorial

Introduction

Here is the source code for com.tnf.ptm.gfx.particle.PartMan.java

Source

/*
 * Copyright 2017 TheNightForum
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.tnf.ptm.gfx.particle;

import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.Vector2;
import com.tnf.ptm.common.PtmColor;
import com.tnf.ptm.common.PtmMath;
import com.tnf.ptm.common.PtmGame;
import com.tnf.ptm.handler.dra.Dra;
import com.tnf.ptm.handler.dra.DraLevel;
import com.tnf.ptm.handler.dra.DrasObject;
import com.tnf.ptm.handler.dra.RectSprite;
import com.tnf.ptm.entities.item.Shield;
import com.tnf.ptm.entities.ship.hulls.Hull;

import java.util.ArrayList;

public class PartMan {
    public static final float EXPL_LIGHT_MAX_SZ = .4f;
    public static final float EXPL_LIGHT_MAX_FADE_TIME = .8f;
    public static final float SZ_TO_BLINK_COUNT = 18f;

    public PartMan() {
    }

    public void finish(PtmGame game, ParticleSrc src, Vector2 basePos) {
        if (src.isContinuous()) {
            src.setWorking(false);
        }
        ArrayList<Dra> dras = new ArrayList<Dra>();
        dras.add(src);
        DrasObject o = new DrasObject(dras, new Vector2(basePos), new Vector2(), null, true, false);
        game.getObjMan().addObjDelayed(o);
    }

    public void blinks(Vector2 pos, PtmGame game, float sz) {
        int count = (int) (SZ_TO_BLINK_COUNT * sz * sz);
        for (int i = 0; i < count; i++) {
            Vector2 lightPos = new Vector2();
            PtmMath.fromAl(lightPos, PtmMath.rnd(180), PtmMath.rnd(0, sz / 2));
            lightPos.add(pos);
            float lightSz = PtmMath.rnd(.5f, 1) * EXPL_LIGHT_MAX_SZ;
            float fadeTime = PtmMath.rnd(.5f, 1) * EXPL_LIGHT_MAX_FADE_TIME;
            LightObject light = new LightObject(game, lightSz, true, 1, lightPos, fadeTime, game.getCols().fire);
            game.getObjMan().addObjDelayed(light);
        }
    }

    public void shieldSpark(PtmGame game, Vector2 collPos, Hull hull, TextureAtlas.AtlasRegion shieldTex,
            float perc) {
        if (perc <= 0) {
            return;
        }
        Vector2 pos = hull.getPos();
        float angle = PtmMath.angle(pos, collPos);
        float sz = hull.config.getSize() * Shield.SIZE_PERC * 2;
        float alphaSum = perc * 3;
        RectSprite s = null;
        int count = (int) alphaSum + 1;
        for (int i = 0; i < count; i++) {
            s = blip(game, pos, angle, sz, .5f, hull.getSpd(), shieldTex);
        }
        float lastTint = PtmMath.clamp(alphaSum - (int) alphaSum);
        if (s != null) {
            s.tint.a = lastTint;
            s.baseAlpha = lastTint;
        }
    }

    public RectSprite blip(PtmGame game, Vector2 pos, float angle, float sz, float fadeTime, Vector2 spd,
            TextureAtlas.AtlasRegion tex) {
        RectSprite s = new RectSprite(tex, sz, 0, 0, new Vector2(), DraLevel.PART_FG_0, angle, 0, PtmColor.WHITE,
                true);
        ArrayList<Dra> dras = new ArrayList<Dra>();
        dras.add(s);
        DrasObject o = new DrasObject(dras, new Vector2(pos), new Vector2(spd), null, false, false);
        o.fade(fadeTime);
        game.getObjMan().addObjDelayed(o);
        return s;
    }

}