#include <curses.h>
#include "block.h"
#include "console.h"
#include "map.h"
#include "wall.h"
#include "main.h"
#include <assert.h>
#include <iostream>
#include <fstream>

using namespace std;

block::block() {
	setchar('#');
	setcol(GREEN);
	settype("block");
}

// -1 means couldn't be pushed, >=0 means it got pushed
int block::push(int x, int y, int d) {
	// the block has no data members storing it's location, so we have to
	// tell it where it is.
	// and some dummyproofing...
	if (m->spriteat(x, y) == NULL) return -1;
	if (m->spriteat(x, y)->mytype() != "block") return -1;
	x += DIRX[d]; y += DIRY[d];
	if (!m->inbounds(x, y)) return -1;
	sprite* s = m->spriteat(x, y);
	if (s == NULL) {
		m->setsprite(x, y, this);
		return 0;
	}
	if (s->mytype() == "block") {
		return push(x, y, d);
	}
	if (s->mytype() == "wall") {
		if (((wall*)s)->isexplode()) return 0;
		else return -1;
	}

	int t = s->squish(d);
	if (t >= 0) {
		m->setsprite(x, y, this);
	}
	return t;
}
