#ifndef PLAYCANVAS_H
#define PLAYCANVAS_H

#include <qcanvas.h>
#include <qptrlist.h>

#include "level.h"

class QPixmap;
class QTimer;
class Bomb;
class Player;
class Blast;
class Item;
class Exit;
class AI_Player;
class Resource;
class Rule;

class PlayCanvas : public QCanvas
{
	Q_OBJECT
public:
	PlayCanvas(QObject *parent = NULL, const char *name = NULL);
	~PlayCanvas();

	enum Events
	{
		moveEvent,
		fireEvent,
		timebombEvent
	};
	enum Options
	{
		optNone,
		optLeft,
		optRight,
		optDown,
		optUp
	};
	enum OverlayFonts
	{
		loser,
		winner,
		pause,
		none
	};

	void buildLevel(QString levelfile);
	void drawBackground(bool only, int only_x, int only_y);
	void placeDynamic(void);
	void createMonster(int count = 0);
	void placeBomb(Player *p);
	void placeBomb(Player *p, int id, int x, int y);
	void checkItem(/*int x, int y*/Player *p, bool isplayer);
	bool action(int event, int option);
	bool action_internal(int event, int option);
	bool walkableTile(int fromx, int fromy, int x, int y);

	void setTheme(QString theme);
	void setPlayers(int players);
	void setRules(Rule *rules);

	void enable(bool enabled);
	void enableLocal(bool enabled);
	bool enabled();

	void selectWeapon();

	void createExit(int x, int y);
	void createItem(int type, int id, int x, int y);
	void createMonster(int id, int x, int y);
	void createPlayer(int id, int x, int y);
	void createBomb(int playerid, int id, int x, int y);
	bool createBlast(int id, int x, int y);

	void actionItem(int id, int type, int count);

	void removeItem(int id);
	void removeMonster(int id);
	void removePlayer(int id);
	void removeBomb(int id);
	void removeBlast(int id);

	void activateItem(int id);
	void activateExit();
	void highlightItem(int id);
	void highlightExit();

	void moveMonster(int id, int direction);
	int monsterWaypoints(AI_Player *monster, int x, int y);

	void setOverlay(int overlay);

	Level *level();

public slots:
	void monsterTimeout();
	void clockTimeout();

signals:
	void playerKilled();
	void levelFinished();
	void information(int lives, int bombs, int power);
	void informationScore(int time, int score);
	void position(int x, int y);

protected:
	void timerEvent(QTimerEvent *e);

private:
	int random(int max);
	QPoint goodrandom(int max);
	int isgoodrandom(int x, int y);

	Item *findItem(int x, int y);
	QString weapontype(int weaponcounter);

	Level *m_level;

	QPtrList<Bomb> bombList;
	QPtrList<Item> itemList;
	QPtrList<Blast> blastList;
	QPtrList<Player> playerList;
	QPtrList<AI_Player> monsterList;

	QTimer *monsterTimer;
	QTimer *clockTimer;

	Bomb *weapon;
	int weaponcounter;

	Player *currPlayer;
	Resource *resource;
	Exit *exit;
	QCanvasText *overlayfont;
	bool m_away;
	int m_players;
	bool m_enabled;
	bool m_localenabled;
	Rule *m_rules;
	int m_score, m_clock;
};

#endif
