/* Copyright 2015 Dietrich Epp.

   This file is part of Dash and the Jetpack Space Pirates.  The Dash
   and the Jetpack Space Pirates source code is distributed under the
   terms of the MIT license.  See LICENSE.txt for details. */
'use strict';

var glm = require('gl-matrix');
var vec2 = glm.vec2;

var color = require('./color');
var entity = require('./entity');
var physics = require('./physics');

var BobPeriod = 2;
var BobDistance = 1;

/*
 * Base mixin for items.
 */
var Item = {
	spawn: function(game, args) {
		var position = args.position;
		var body = new p2.Body({
			position: args.position,
			mass: this.mass,
			fixedRotation: true,
		});
		var shape = new p2.Circle({
			radius: 3,
			sensor: true,
		});
		shape.collisionGroup = physics.Mask.Item;
		shape.collisionMask = physics.Mask.Player;
		body.addShape(shape);
		this.body = body;
		this.pos = vec2.create();
		this.bobShift = Math.random() * (Math.PI * 2);
	},
	emit: function(game) {
		vec2.copy(this.pos, this.body.interpolatedPosition);
		this.pos[1] += (0.5 * BobDistance) *
			Math.sin(game.time.elapsed * (2 * Math.PI / BobPeriod) + this.bobShift);
		game.sprites.add({
			position: this.pos,
			radius: 3,
			color: this.color,
			sprite: 'IShield2',
		});
	},
	onContact: function(game, eq, body) {
		console.log('PICKUP');
	},
	color: color.hex(0xffffff),
	sprite: 'PHurt',
};

entity.registerTypes(null, {
	Item: Item,
});

@@ -295,16 +295,37 @@
 lm.vec2;
+%0Avar vec4 = glm.vec4;
 %0A%0Avar co
@@ -421,16 +421,34 @@
 ics');%0A%0A
+var ItemSize = 3;%0A
 var BobP
@@ -480,16 +480,888 @@
 e = 1;%0A%0A
+var bob = vec2.create();%0Afunction getBobPos(obj, game) %7B%0A%09vec2.copy(bob, obj.body.interpolatedPosition);%0A%09bob%5B1%5D += (0.5 * BobDistance) *%0A%09%09Math.sin(game.time.elapsed * (2 * Math.PI / BobPeriod) + obj.bobShift);%0A%09return bob;%0A%7D%0A%0A/*%0A * Item %22corpse%22%0A*/%0Avar Corpse = %7B%0A%09spawn: function(game, args) %7B%0A%09%09var base = args.base;%0A%09%09this.body = base.body;%0A%09%09this.sprite = base.sprite;%0A%09%09this.bobShift = base.bobShift;%0A%09%09this.color = vec4.clone(base.color);%0A%09%09this.radius = 3;%0A%09%09_.forEach(this.body.shapes, function(s) %7B%0A%09%09%09s.contactGroup = 0;%0A%09%09%09s.contactMask = 0;%0A%09%09%7D);%0A%09%09game.tween(this)%0A%09%09%09.to(%7B%0A%09%09%09%09radius: ItemSize * 5,%0A%09%09%09%09color: color.Transparent%0A%09%09%09%7D, this.lifetime, 'SineIn')%0A%09%09%09.start();%0A%09%7D,%0A%09emit: function(game) %7B%0A%09%09game.sprites.add(%7B%0A%09%09%09position: getBobPos(this, game),%0A%09%09%09radius: this.radius,%0A%09%09%09color: this.color,%0A%09%09%09sprite: 'IShield2',%0A%09%09%7D);%0A%09%7D,%0A%09lifetime: 0.3,%0A%7D;%0A%0A
 /*%0A * Ba
@@ -1602,25 +1602,32 @@
 %0A%09%09%09radius: 
-3
+ItemSize
 ,%0A%09%09%09sensor:
@@ -1776,36 +1776,8 @@
 dy;%0A
-%09%09this.pos = vec2.create();%0A
 %09%09th
@@ -1853,178 +1853,8 @@
 ) %7B%0A
-%09%09vec2.copy(this.pos, this.body.interpolatedPosition);%0A%09%09this.pos%5B1%5D += (0.5 * BobDistance) *%0A%09%09%09Math.sin(game.time.elapsed * (2 * Math.PI / BobPeriod) + this.bobShift);%0A
 %09%09ga
@@ -1887,16 +1887,29 @@
 on: 
-this.pos
+getBobPos(this, game)
 ,%0A%09%09
@@ -1917,17 +1917,24 @@
 radius: 
-3
+ItemSize
 ,%0A%09%09%09col
@@ -2046,16 +2046,69 @@
 CKUP');%0A
+%09%09game.spawn(%7B%0A%09%09%09type: Corpse,%0A%09%09%09base: this,%0A%09%09%7D);%0A
 %09%7D,%0A%09col

add item pickup animation