"use strict";

// dx and dy are in 1/8 units
var GO_Bullet = function(dx,dy){
	this.sprite = new Sprite(GO_Bullet.spriteSheet,"Bullet");
	this.sprite.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE |
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_ACTIVE | // so it can hit solid e.g. carried blocks
		DEFINE_SPRITE_COLLISION_FLAGS.GROUND;
	this.sprite.setBoundingBox(
		2,
		2,
		5,
		5);
	//sprite.constraints=constraints;
	this.sprite.max_step = 0;
	this.sprite.game_object = this; // no longer a generic GO

	this.dx = dx;
	this.dy = dy;
	this.dx_accum = 0;
	this.dy_accum = 0;

};
GO_Bullet.spriteImg = SDS_Load_Image("art/sheets/sprite/bullet1.png");
GO_Bullet.spriteSheet = new TileSheet(3, 3, 2, 0, GO_Bullet.spriteImg);

GO_Bullet.prototype = new GameObject(
	new Sprite(
		GO_Bullet.spriteSheet,
		"Bullet"
	),
	"Bullet"
	);

	GO_Bullet.prototype.interactions = 
		DEFINE_GAMEOBJECT_INTERACTIONS.INFLICT;

	GO_Bullet.prototype.inflict = function(){
		this.sprite.map_room.removeSprite(this.sprite);
		this.manager.remove(this);

		return {hp: 1, chi:0};
	};

	GO_Bullet.prototype.Update = function(){
		this.dx_accum+=this.dx;
		this.dy_accum+=this.dy;

		this._heap.dx = this.dx_accum>>3;
		this._heap.dy = this.dy_accum>>3;

		this.dx_accum &= 7;
		this.dy_accum &= 7;

		this.sprite.move(this._heap.dx,this._heap.dy,  this._heap.spriteMoveRes);


		if (this._heap.spriteMoveRes.flags & (DEFINE_SPRITE_MOVE_RESULT_EJECTED_H | DEFINE_SPRITE_MOVE_RESULT_EJECTED_V)){
			this.sprite.map_room.removeSprite(this.sprite);
			this.manager.remove(this);
		}

		this.sprite.frame++;
		if (this.sprite.frame>2) this.sprite.frame=0;
	};

