view ships/asteroids.d @ 24:441eb7672404

impleneted steer to avoid
author zzzzrrr <mason.green@gmail.com>
date Fri, 27 Mar 2009 16:05:24 -0400
parents 4fce5596d1f6
children 2bf818f8b005
line wrap: on
line source

/*
 *  Copyright (c) 2009 Blaze Contributors http://www.dsource.org/projects/blaze
 *  Maintained by Mason Green (zzzzrrr)
 *
 *   This software is provided 'as-is', without any express or implied
 *   warranty. In no event will the authors be held liable for any damages
 *   arising from the use of this software.
 *
 *   Permission is granted to anyone to use this software for any purpose,
 *   including commercial applications, and to alter it and redistribute it
 *   freely, subject to the following restrictions:
 *
 *   1. The origin of this software must not be misrepresented; you must not
 *   claim that you wrote the original software. If you use this software
 *   in a product, an acknowledgment in the product documentation would be
 *   appreciated but is not required.
 *
 *   2. Altered source versions must be plainly marked as such, and must not be
 *   misrepresented as being the original software.
 *
 *   3. This notice may not be removed or altered from any source
 *   distribution.
 */
module openmelee.ships.asteroids;

import blaze.dynamics.bzBody : bzBody;
import blaze.bzWorld: bzWorld;
import blaze.dynamics.bzBodyDef;
import blaze.collision.shapes.bzCircle : bzCircleDef;
import blaze.collision.shapes.bzPolygon : bzPolyDef;
import blaze.collision.shapes.bzShape : bzShapeDef;
import blaze.common.bzMath: bzVec2, bzMul, bzXForm;
import blaze.dynamics.forces.bzAttractor: bzAttractor;


import openmelee.ai.utilities;

const PI = 3.141593;

class Asteroid
{

    bzBodyDef bd;
    bzBody rBody;
    bzShapeDef sd;

    bzWorld world;

    this(bzWorld world) {
        this.world = world;
        init();
    }

    void init() {
        
        float minRadius = 0.1;
        float maxRadius = 10;
        float strength = 0.5f;
        bzVec2 center = bzVec2(0,0);

		{
		    float radius = 0.5f;
            float density = 5.0f;
			auto sd1 = new bzCircleDef(density, radius);
			sd1.localPosition.set(-0.5f, 0.5f);

            density = 0.0f;
			auto sd2 = new bzCircleDef(density, radius);
			sd2.localPosition.set(0.5f, 0.5f);

			for (int i = 0; i < 5; ++i)
			{
				float x = randomRange(-100f, 100f);
                float y = randomRange(-100f, 100f);
				bzVec2 position = bzVec2(x , y);
				float angle = randomRange(-PI, PI);
				bd = new bzBodyDef(position, angle);
                bd.allowFreeze = false;
                bd.allowSleep = false;
				rBody = world.createBody(bd);
				rBody.createShape(sd1);
				rBody.createShape(sd2);
				rBody.setMassFromShapes();
                auto attractor = new bzAttractor(rBody, center, strength, minRadius, maxRadius);
                world.addForce(attractor);
                rBody.linearVelocity = bzVec2(x*0.1, y*0.1);
			}
		}

		{
			auto sd1 = new bzPolyDef();
			sd1.setAsBox(0.25f, 0.5f);
			sd1.density = 5.0f;

			auto sd2 = new bzPolyDef();
			sd2.setAsBox(0.25f, 0.5f, bzVec2(0.0f, -0.5f), 0.5f * PI);
			sd2.density = 5.0f;

			for (int i = 0; i < 5; ++i)
			{
				float x = randomRange(-100f, 100f);
                float y = randomRange(-100f, 100f);
				bzVec2 position = bzVec2(x , y);
				float angle = randomRange(-PI, PI);
				bd = new bzBodyDef(position, angle);
                bd.allowFreeze = false;
                bd.allowSleep = false;
				rBody = world.createBody(bd);
				rBody.createShape(sd1);
				rBody.createShape(sd2);
				rBody.setMassFromShapes();
                auto attractor = new bzAttractor(rBody, center, strength, minRadius, maxRadius);
                world.addForce(attractor);
                rBody.linearVelocity = bzVec2(x*0.1, y*0.1);
			}
		}

		{
			bzXForm xf1;
			xf1.R.set(0.3524f * PI);
			xf1.position = bzMul(xf1.R, bzVec2(1.0f, 0.0f));

			auto sd1 = new bzPolyDef();
			sd1.vertices.length = 3;
			sd1.vertices[0] = bzMul(xf1, bzVec2(-1.0f, 0.0f));
			sd1.vertices[1] = bzMul(xf1, bzVec2(1.0f, 0.0f));
			sd1.vertices[2] = bzMul(xf1, bzVec2(0.0f, 0.5f));
			sd1.density = 5.0f;

			bzXForm xf2;
			xf2.R.set(-0.3524f * PI);
			xf2.position = bzMul(xf2.R, bzVec2(-1.0f, 0.0f));

			auto sd2 = new bzPolyDef();
			sd2.vertices.length = 3;
			sd2.vertices[0] = bzMul(xf2, bzVec2(-1.0f, 0.0f));
			sd2.vertices[1] = bzMul(xf2, bzVec2(1.0f, 0.0f));
			sd2.vertices[2] = bzMul(xf2, bzVec2(0.0f, 0.5f));
			sd2.density = 5.0f;

			for (int i = 0; i < 5; ++i)
			{
				float x = randomRange(-100f, 100f);
                float y = randomRange(-100f, 100f);
				bzVec2 position = bzVec2(x , y);
				float angle = 0.0f;
				bd = new bzBodyDef(position, angle);
                bd.allowFreeze = false;
                bd.allowSleep = false;
				rBody = world.createBody(bd);
				rBody.createShape(sd1);
				rBody.createShape(sd2);
				rBody.setMassFromShapes();
                auto attractor = new bzAttractor(rBody, center, strength, minRadius, maxRadius);
                world.addForce(attractor);
                rBody.linearVelocity = bzVec2(x*0.1, y*0.1);
			}
		}
	}
}