view melee.d @ 4:8d7c50415269

changed Hz
author Zzzzrrr <mason.green@gmail.com>
date Fri, 20 Mar 2009 17:00:30 -0400
parents a40d066ebbd1
children 6f455ef24063
line wrap: on
line source

/*
 * Copyright (c) 2009, Mason Green (zzzzrrr)
 * 
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * * Neither the name of the polygonal nor the names of its contributors may be
 *   used to endorse or promote products derived from this software without specific
 *   prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
module openmelee.melee;

import Integer = tango.text.convert.Integer;
import tango.math.Math;
import tango.math.random.Kiss;

import xf.hybrid.Event;
import xf.input.KeySym;
import xf.omg.core.LinearAlgebra;

public import blaze.all;

import openmelee.boundaryListener;
import openmelee.contactListener;
import openmelee.ship;
import openmelee.urQuan;
import openmelee.orz;

// Cursor scale factor
const CURSORSIZE = 0.05f;

const INIT_SPAWN_SIZE = 0.5f;

// Dragging stuffs
const BUNGEE_K = 1.5f;
// Damping factor for dragging
const DRAGDAMP = 20.0f;

// Size of hinges
const HINGE_RADIUS = 0.05f;

// Smallest allowed dimension
const MIN_DIMENSION = 0.1;

// Water stuffs
const MAX_PARTICLES = 10000;
const WATER_BOUNCE = 0.01;
const WATER_FRICTION = 0.5;
const WATER_RADIUS = 0.05f;
const MAX_CIRCLE_RES = 32;

const k_maxContactPoints = 2048;

enum ContactState {
    e_contactAdded,
    e_contactPersisted,
    e_contactRemoved
}

struct ContactPoint {
    bzShape shape1;
    bzShape shape2;
    bzVec2 normal;
    bzVec2 position;
    bzVec2 velocity;
    bzContactID id;
    ContactState state;
}

// Melee settings. Some can be controlled in the GUI.
struct Settings {
    float hz = 60;
    int velocityIterations = 8;
    int positionIterations = 2;
    bool drawShapes = true;
    bool drawJoints = true;
    bool drawControllers;
    bool drawCoreShapes;
    bool drawAABBs = true;
    bool drawOBBs;
    bool drawPairs;
    bool drawContactPoints;
    bool drawContactNormals;
    bool drawContactForces;
    bool drawFrictionForces;
    bool drawCOMs;
    bool drawStats;
    bool enableWarmStarting;
    bool enableTOI;
}

// Dirty, dirty hack for communicating config changes to Main
// TODO: Harass h3 to add .changed to hybrid so this isn't necessary
struct ConfigChange (T)
{
    protected
    {
        bool _pending = false;
        T _value;
    }

    T value()
    {
        _pending = false;
        return _value;
    }

    void value(T change)
    {
        _value = change;
        _pending = true;
    }

    bool pending()
    {
        return _pending;
    }

    T opAssign(T change)
    {
        value(change);
        return _value;
    }

    bool opEquals(T other)
    {
        return _value == other;
    }
}

T randomRange(T = int) (T min, T max)
{
    return min + Kiss.instance.natural() % (max + 1 - min);
}

class Melee
{

	this(Settings *settings)
	{
		this.settings = settings;
		init();
		spawnRect = vec2(INIT_SPAWN_SIZE, INIT_SPAWN_SIZE);
		// bzWorld boundary callback
		m_boundaryListener = new BoundaryListener(this);
		// bzContact callback
		m_contactListener = new ContactListener(this);
	}

	void init() {
	    // Define world boundaries
		worldAABB.lowerBound.set(-75.0f, -75.0f);
		worldAABB.upperBound.set(75.0f, 75.0f);
		world = new bzWorld(worldAABB, gravity, allowSleep);
		world.boundaryListener = m_boundaryListener;
		world.contactListener = m_contactListener;
		viewCenter = vec2(10, 10);
		ship1 = new Orz(world);
		ship2 = new UrQuan(world);
	}

	void drag()
	{

	}

	EventHandling onClick(MouseButtonEvent e)
	{
        return EventHandling.Stop;
	}

	EventHandling onKey(KeyboardEvent e)
	{
		if (e.unicode == '+') {         // HACK: the windows input writer doesn't do plus correctly yet   - h3
			e.keySym = KeySym.plus;
		}
		if (e.down) {
			switch (e.keySym) {
			case KeySym.Escape:
                quit = true;
                break;
			default:
			    char key = cast(char) e.keySym;
			    if(key == 'w') {
			        thrust = true;
			        break;
			    } else {
			        ship1.turn(key);
			    }
				break;
			}
        // Key released
		} else {
		    char key = cast(char) e.keySym;
		    if(key == 'w') {
		         thrust = false;
		    } else if (key == 'd' || key == 'a') {
                ship1.rBody.angularVelocity = 0.0f;
            }
		}

		return EventHandling.Stop;
	}

	// Mouse move
	EventHandling onMove(MouseMoveEvent e)
	{
		return EventHandling.Stop;
	}

	EventHandling onDT(TimeUpdateEvent e)
	{
		return EventHandling.Continue;
	}

	EventHandling onMouseEnter(MouseEnterEvent e)
	{
		return EventHandling.Continue;
	}

	EventHandling onMouseLeave(MouseLeaveEvent e)
	{
		return EventHandling.Continue;
	}

	protected
	{
		const bzVec2 gravity = bzVec2(0.0f, 0.0f);
		bool allowSleep = false;

		vec2 spawnRect;

		vec2[] drawing;

		vec2i screenSize = vec2i.zero;
		vec2 mousePos = vec2.zero;

		bool scaling = false;
		bool full = false;

		vec2 spawnStart;

		bool preserveBullet = false;

		float waterDelta = 0;
	}

	void boundaryViolated(bzBody rBody)
	{
        uint key = rBody.toHash();
        wrapList[key] = rBody;
	}

	bool quit;

	// Ortho view zoom
	float zoom = 40;
	int pointCount;
	vec2 viewCenter;
    bzBody[uint] wrapList;

	bzWorld world;
	Settings *settings;

	// bzWorld boundary listener. Destroy bodies that leave world bzAABB
	bzBoundaryListener m_boundaryListener;
	bzContactListener m_contactListener;
	ContactPoint[k_maxContactPoints] points;

	ConfigChange!(vec2) editChange;
    bool thrust;
    bzAABB worldAABB;
    
	Ship ship1;
	Ship ship2;
}

// Utility functions
bzVec2 toBlaze(vec2 vec)
{
    auto ret = bzVec2(vec.x, vec.y);
    return ret;
}

vec2 rotate(vec2 point, float rad)
{
    return vec2(cos(rad) * point.x - sin(rad) * point.y, sin(rad) * point.x + cos(rad) * point.y);
}

class Select
{
	bool select;
}