<commit_before>/// <reference path="../../typings/index.d.ts" />
/// <reference path="../../node_modules/excalibur/dist/excalibur.d.ts" />

import * as ex from "excalibur";
import { Engine, IEngineOptions, Texture } from "excalibur";
import * as p2 from 'p2';

var game = new Engine({
    width: 640,
    height: 480
});

// Setup input
// IEngineOptions.pointerScope = ex.Input.PointerScope.Canvas;

// Load textures
var txCursor:Texture = new Texture("./assets/materials/crosshairs.png");
var txGun:Texture = new Texture("./assets/materials/stoner63.png");
var txCasing:Texture = new Texture("./assets/materials/casing.png");
var loader = new ex.Loader([txCursor, txGun, txCasing]);

// Setup physics world
var world = new p2.World({
    gravity:[0, 10] // minus values cause things to fall upwards
});
var fixedTimeStep = 1 / 60; // seconds
var maxSubSteps = 10; // Max sub steps to catch up with the wall clock
var bodies_simulated:Map<number, p2.Body> = new Map<number, p2.Body>();
var bodies_unsimulated:Map<number, p2.Body> = new Map<number, p2.Body>();

var groundBody = new p2.Body({
    mass: 0, // Setting mass to 0 makes it static
    position: [0, game.getHeight()],
    angle: Math.PI
});
var groundShape = new p2.Plane();
groundBody.addShape(groundShape);
world.addBody(groundBody);

// Start the engine to begin the game.
game.start(loader).then(() => {
    // Crosshairs
    var cursorSprite:ex.Sprite = txCursor.asSprite();
    var cursor = new ex.Actor(game.getWidth() / 2, game.getHeight() / 2, cursorSprite.width, cursorSprite.height);
    setDimensions(cursor, 64, 0);
    matchDimensions(cursorSprite, cursor);
    cursor.addDrawing(cursorSprite);
    game.add(cursor);

    // Gun
    var gunSprite:ex.Sprite = txGun.asSprite();
    var gun = new ex.Actor(100, game.getHeight() / 2, gunSprite.width, gunSprite.height);
    var gunPort = new ex.Actor(-16, -14, 1, 1);
    gun.add(gunPort);
    setDimensions(gun, 180, 0);
    matchDimensions(gunSprite, gun);
    gun.addDrawing(gunSprite);
    game.add(gun);
    
    let gunShape:p2.Shape;
    gunShape = new p2.Box({ width: gun.getWidth(), height: gun.getHeight() });
    let gunBody = createBodyFromActor(gun, 0);
    gunBody.addShape(gunShape);
    world.addBody(gunBody);
    bodies_unsimulated.set(gun.id, gunBody);

    // Casing
    var casingActors:ex.Actor[] = [];
    var casingSprite:ex.Sprite = txCasing.asSprite();

    game.input.pointers.primary.on('move', (evt:ex.Input.PointerEvent) => {
        // Crosshairs on cursor
        cursor.pos.x = evt.x;
        cursor.pos.y = evt.y;

        // Gun points at cursor
        var diffVec:ex.Vector = new ex.Vector(evt.x - gun.x, evt.y - gun.y)
        gun.rotation = diffVec.toAngle();
    });

    game.input.pointers.primary.on('up', (evt:ex.Input.PointerEvent) => {
        let casing = new ex.Actor(gunPort.getWorldPos().x - 15, gunPort.getWorldPos().y - 15, casingSprite.width, casingSprite.height);
        setDimensions(casing, 10, 0);
        matchDimensions(casingSprite, casing);
        casing.addDrawing(casingSprite);

        // Set motion that will be picked up by p2
        let velocity:ex.Vector = new ex.Vector(-1, -50);
        velocity = velocity.rotate(gunPort.getWorldRotation(), ex.Vector.Zero);
        casing.vel.setTo(velocity.x, velocity.y);
        casing.rotation = gunPort.getWorldRotation();
        //casing.rx = -0.5;
        

        setPhysics(casing, SupportedShape.Box, 0.1);

        game.add(casing);
        casingActors.push(casing);
        if (casingActors.length > 10) {
            var oldestCasing:ex.Actor = casingActors.shift();
            oldestCasing.kill();
        }
    });

    game.on('postupdate', (evt:ex.PostUpdateEvent) => {
        // Step physics simulation
        world.step(fixedTimeStep, evt.delta, maxSubSteps);

        // Update actors in world
        for (let actor of game.currentScene.children) {
            // If actor is simulated, p2 position => ex position
            if (bodies_simulated.has(actor.id)) {
                let actorPhysicsBody:p2.Body = bodies_simulated.get(actor.id)
                // Excalibur has reversed Y axis
                actor.pos.setTo(actorPhysicsBody.interpolatedPosition[0], actorPhysicsBody.interpolatedPosition[1]);
                actor.rotation = actorPhysicsBody.interpolatedAngle;

                // Do not use Excalibur delta-pos or delta-rotation
                actor.vel.setTo(0, 0);
                actor.rx = 0;
            }
            // If actor not simulated, ex position => p2 position
            if (bodies_unsimulated.has(actor.id)) {
                let actorPhysicsBody:p2.Body = bodies_unsimulated.get(actor.id)

                let actorWorldPos:ex.Vector = actor.getWorldPos();
                actorPhysicsBody.position = [actorWorldPos.x, actorWorldPos.y];
                actorPhysicsBody.angle = actor.rotation;

            }
        }
    });
});

function matchDimensions(sprite:ex.Sprite, actor:ex.Actor):void {<commit_msg>Converted unsimulated bodies to simulated.<commit_after>/// <reference path="../../typings/index.d.ts" />
/// <reference path="../../node_modules/excalibur/dist/excalibur.d.ts" />

import * as ex from "excalibur";
import { Engine, IEngineOptions, Texture } from "excalibur";
import * as p2 from 'p2';

var game = new Engine({
    width: 640,
    height: 480
});

// Setup input
// IEngineOptions.pointerScope = ex.Input.PointerScope.Canvas;

// Load textures
var txCursor:Texture = new Texture("./assets/materials/crosshairs.png");
var txGun:Texture = new Texture("./assets/materials/stoner63.png");
var txCasing:Texture = new Texture("./assets/materials/casing.png");
var loader = new ex.Loader([txCursor, txGun, txCasing]);

// Setup physics world
var world = new p2.World({
    gravity:[0, 10] // minus values cause things to fall upwards
});
var fixedTimeStep = 1 / 60; // seconds
var maxSubSteps = 10; // Max sub steps to catch up with the wall clock
var bodies_simulated:Map<number, p2.Body> = new Map<number, p2.Body>();

var groundBody = new p2.Body({
    mass: 0, // Setting mass to 0 makes it static
    position: [0, game.getHeight()],
    angle: Math.PI
});
var groundShape = new p2.Plane();
groundBody.addShape(groundShape);
world.addBody(groundBody);

// Start the engine to begin the game.
game.start(loader).then(() => {
    // Crosshairs
    var cursorSprite:ex.Sprite = txCursor.asSprite();
    var cursor = new ex.Actor(game.getWidth() / 2, game.getHeight() / 2, cursorSprite.width, cursorSprite.height);
    setDimensions(cursor, 64, 0);
    matchDimensions(cursorSprite, cursor);
    cursor.addDrawing(cursorSprite);
    game.add(cursor);

    // Gun
    var gunSprite:ex.Sprite = txGun.asSprite();
    var gun = new ex.Actor(100, game.getHeight() / 2, gunSprite.width, gunSprite.height);
    var gunPort = new ex.Actor(-16, -14, 1, 1);
    gun.add(gunPort);
    setDimensions(gun, 180, 0);
    matchDimensions(gunSprite, gun);
    gun.addDrawing(gunSprite);
    setPhysics(gun, SupportedShape.Box, 0);
    game.add(gun);
    
    let gunShape:p2.Shape;
    gunShape = new p2.Box({ width: gun.getWidth(), height: gun.getHeight() });
    let gunBody = createBodyFromActor(gun, 0);
    gunBody.addShape(gunShape);
    world.addBody(gunBody);

    // Casing
    var casingActors:ex.Actor[] = [];
    var casingSprite:ex.Sprite = txCasing.asSprite();

    game.input.pointers.primary.on('move', (evt:ex.Input.PointerEvent) => {
        // Crosshairs on cursor
        cursor.pos.x = evt.x;
        cursor.pos.y = evt.y;

        // Gun points at cursor
        var diffVec:ex.Vector = new ex.Vector(evt.x - gun.x, evt.y - gun.y)
        bodies_simulated.get(gun.id).angle = diffVec.toAngle();
    });

    game.input.pointers.primary.on('up', (evt:ex.Input.PointerEvent) => {
        let casing = new ex.Actor(gunPort.getWorldPos().x - 15, gunPort.getWorldPos().y - 15, casingSprite.width, casingSprite.height);
        setDimensions(casing, 10, 0);
        matchDimensions(casingSprite, casing);
        casing.addDrawing(casingSprite);

        // Set motion that will be picked up by p2
        let velocity:ex.Vector = new ex.Vector(-1, -50);
        velocity = velocity.rotate(gunPort.getWorldRotation(), ex.Vector.Zero);
        casing.vel.setTo(velocity.x, velocity.y);
        casing.rotation = gunPort.getWorldRotation();
        //casing.rx = -0.5;
        

        setPhysics(casing, SupportedShape.Box, 0.1);

        game.add(casing);
        casingActors.push(casing);
        if (casingActors.length > 10) {
            var oldestCasing:ex.Actor = casingActors.shift();
            oldestCasing.kill();
        }
    });

    game.on('postupdate', (evt:ex.PostUpdateEvent) => {
        // Step physics simulation
        world.step(fixedTimeStep, evt.delta, maxSubSteps);

        // Update actors in world
        for (let actor of game.currentScene.children) {
            // If actor is simulated, p2 position => ex position
            if (bodies_simulated.has(actor.id)) {
                let actorPhysicsBody:p2.Body = bodies_simulated.get(actor.id)
                // Excalibur has reversed Y axis
                actor.pos.setTo(actorPhysicsBody.interpolatedPosition[0], actorPhysicsBody.interpolatedPosition[1]);
                actor.rotation = actorPhysicsBody.interpolatedAngle;

                // Do not use Excalibur delta-pos or delta-rotation
                actor.vel.setTo(0, 0);
                actor.rx = 0;
            }
        }
    });
});

function matchDimensions(sprite:ex.Sprite, actor:ex.Actor):void {
{'fraction_non_alphanumeric': 0.13845350052246605, 'fraction_numerical': 0.015569487983281087, 'mean_word_length': 2.7299298519095867, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1737615', 'n_tokens_mistral': 3192, 'n_tokens_neox': 3065, 'n_words': 676}