view melee/melee.d @ 28:1cc6b8c0acd2

fixed window close
author zzzzrrr <mason.green@gmail.com>
date Mon, 30 Mar 2009 12:48:45 -0400
parents d63faa81a5e4
children 55eb66672c03
line wrap: on
line source

/*
 * Copyright (c) 2009, Mason Green (zzzzrrr)
 * http://www.dsource.org/projects/openmelee
 * 
 * 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.melee;

import tango.io.Stdout;
import tango.util.container.LinkedList : LinkedList;

version(distrib) import tango.io.vfs.ZipFolder;
import tango.time.StopWatch;
import fc = tango.text.convert.Float : toString;
import tango.util.log.Trace;

import blaze.common.bzMath : bzVec2;
import blaze.bzWorld : bzWorld;
import blaze.collision.bzCollision : bzAABB;

import openmelee.melee.boundaryListener;
import openmelee.melee.contactListener;
import openmelee.render.render;
import openmelee.ai.ai : AI;
import openmelee.ai.human : Human;
import openmelee.ships.urQuan : UrQuan;
import openmelee.ships.orz : Orz;
import openmelee.ships.planet : Planet;
import openmelee.ships.asteroids : Asteroid;

const ITERS_PER_SECOND = 100;
const k_maxContactPoints = 100;
const NUM_ASTROIDS = 12;

alias LinkedList!(Ship) ObjectList;

// Melee settings
struct Settings {
    float hz = 60;
    int velocityIterations = 3;
    int positionIterations = 1;
    bool drawShapes = true;
    bool drawJoints = true;
    bool drawControllers;
    bool drawCoreShapes;
    bool drawAABBs;
    bool drawOBBs;
    bool drawPairs;
    bool drawContactPoints;
    bool drawContactNormals;
    bool drawContactForces;
    bool drawFrictionForces;
    bool drawCOMs;
    bool drawStats;
    bool enableWarmStarting;
    bool enableTOI;
}

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

class Melee {
    
    ObjectList objectList;
    Settings settings;
    float timeStep;
    const bzVec2 gravity = bzVec2(0.0f, 0.0f);
    bool allowSleep;
    Render render;
    
    AI ai;
    Human human;
    
    Ship ship1;
	Ship ship2;
    Ship planet;
    
    bool running;
    
    StopWatch timer;
    bzAABB worldAABB;
    bzWorld world;
    bzBoundaryListener m_boundaryListener;
	bzContactListener m_contactListener;
	ContactPoint[k_maxContactPoints] points;
    int pointCount;
    
    this() {
        timeStep = settings.hz > 0.0f ? 1.0f / settings.hz : 0.0f;
        objectList = new ObjectList;
		m_boundaryListener = new BoundaryListener(this);
        
        
        initWorld();
        running = true;
        
        human = new Human(ship1, this);
        ai = new AI(ship2, objectList);
        
        render = new Render(world, ship1, ship2, human, settings);
        
        objectList.add(planet);
        objectList.add(ship1);
        objectList.add(ship2);
    }
    
    void run() {
        // Main game loop
        while (running && !human.quit && render.running) {
            
            float delta = timer.stop;
            timer.start;
            
            // Update AI
            ai.move(ship1);
            // Update Physics
            world.step(timeStep, settings.velocityIterations, settings.positionIterations);
            // Update screen
            render.update();
            
            // Limit velocities
            foreach(o; objectList) {
                o.limitVelocity();
                o.updateState();
            }
            
            // Apply thrust
            if(human.thrust && ship1) {
                ship1.thrust();
            }
        }
        
        delete render;
    }

    void initWorld() {
	    // Define world boundaries
		worldAABB.lowerBound.set(-400.0f, -250.0f);
		worldAABB.upperBound.set(400.0f, 250.0f);
		world = new bzWorld(worldAABB, gravity, allowSleep);
		world.boundaryListener = m_boundaryListener;
		ship2 = new UrQuan(world);
		ship1 = new Orz(world);
        ship2.rBody.angle = 3.14159265/4;
        planet = new Planet(world);
        for(int i; i < NUM_ASTROIDS; i++) {
            auto asteroid = new Asteroid(world);
            objectList.add(asteroid);
        }
	}
    
    void boundaryViolated(bzBody rBody)
	{
        float x,y;
        
        if(rBody.position.x > worldAABB.upperBound.x) {
            x = worldAABB.lowerBound.x + 5;
            rBody.position = bzVec2(x, rBody.position.y);
        } else if (rBody.position.x < worldAABB.lowerBound.x) {
            x = worldAABB.upperBound.x - 5;
            rBody.position = bzVec2(x, rBody.position.y);
        } else if (rBody.position.y > worldAABB.upperBound.y) {
            y = worldAABB.lowerBound.y + 5;
            rBody.position = bzVec2(rBody.position.x, y);
        } else if(rBody.position.y < worldAABB.lowerBound.y) {
            y = worldAABB.upperBound.y - 5;
            rBody.position = bzVec2(rBody.position.x, y);
        }        
	}
}