com.dongbat.invasion.util.BulletUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.dongbat.invasion.util.BulletUtil.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.dongbat.invasion.util;

import com.artemis.Entity;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.dongbat.invasion.component.Bullet;
import com.dongbat.invasion.registry.BulletRegistry;

/**
 *
 * @author vuong
 */
public class BulletUtil {

    private static Array<String> slots = new Array<String>();
    private static String currentSlot;

    public static void fire(Vector2 fireVector) {
        int power = UpgradeUtil.getInt("p");
        int multishot = UpgradeUtil.getInt("m");
        float firstAngle = -Constants.BULLET.OFFSET_ANGLE * (multishot - 1) / 2;
        for (int i = 0; i < multishot; i++) {
            Entity bullet = BulletRegistry.createBullet(currentSlot, fireVector);
            float angle = firstAngle + i * Constants.BULLET.OFFSET_ANGLE;
            Vector2 impulse = calculateImpulse(fireVector, power, angle);
            PhysicsUtil.applyImpulse(bullet, impulse);
        }
    }

    private static Vector2 calculateImpulse(Vector2 fireVector, int power, float angle) {
        Vector2 impulse = new Vector2(fireVector);
        // TODO: -40 =.=
        return impulse.scl(power).rotate(angle).scl(-40);
    }

    public static void destroy(Entity bullet) {
        Bullet component = EntityUtil.getComponent(bullet, Bullet.class);
        component.setDeleted(true);
        bullet.deleteFromWorld();
    }

    public Array<String> getSlots() {
        return slots;
    }

    public static void setSlots(Array<String> slots) {
        BulletUtil.slots = slots;
    }

    public static String getCurrentSlot() {
        return currentSlot;
    }

    public static void setCurrentSlot(String currentSlot) {
        BulletUtil.currentSlot = currentSlot;
    }
}