us.notsoserio.ninja.weapons.projectiles.ProjectileFactory.java Source code

Java tutorial

Introduction

Here is the source code for us.notsoserio.ninja.weapons.projectiles.ProjectileFactory.java

Source

/*
 * Copyright 2014 Jesse Corbett
 *
 * 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 us.notsoserio.ninja.weapons.projectiles;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.*;
import us.notsoserio.ninja.entity.Entity;

public class ProjectileFactory {

    public static Projectile CreateProjectile(byte projectileType, Vector2 trajectory, Entity owner,
            World levelWorld) {

        Projectile projectile;

        switch (projectileType) {
        case Projectiles.Kunai:
            projectile = new Kunai(owner, trajectory.angle(), true);
            break;
        default:
            return null;
        }

        projectile.VelocityX = trajectory.x;
        projectile.VelocityY = trajectory.y;
        projectile.X = owner.X + owner.Width / 2;
        projectile.Y = owner.Y + 2 * owner.Height / 3;

        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyDef.BodyType.DynamicBody;
        bodyDef.position.set(new Vector2(projectile.X, projectile.Y));

        Body body = levelWorld.createBody(bodyDef);

        projectile.ApplyBody(body);

        body.setUserData(projectile);
        body.setBullet(true);

        body.setTransform(projectile.X, projectile.Y, projectile.rotation * (float) (Math.PI / 180));
        body.applyLinearImpulse(projectile.VelocityX, projectile.VelocityY, projectile.X, projectile.Y, true);

        if (Gdx.app.getType() != Application.ApplicationType.HeadlessDesktop)
            projectile.CreateDrawable();

        return projectile;
    }

}