/*
 * Avatar.h
 *
 *  Created on: 2009-07-10
 *      Author: wasyl
 */

#ifndef AVATAR_H_
#define AVATAR_H_

#include <allegro5/acodec.h>
#include <allegro5/kcm_audio.h>

#include "../prep/ent/Entity.h"
#include "../prep/res/Sheet.h"
#include "../prep/input/Switch.h"
#include "../prep/misc/Vector2D.h"

class Carryable;
class Avatar : public Entity
{
private:
	enum State {STANDING, RUNNING, FALLING, DUCKING};

public:
	enum WinStateState {NEITHER, DIED, WON};
	struct WinState
	{
		WinStateState state;
		std::string comment;
	};

protected:
	Sheet *mSprite;
	ALLEGRO_SAMPLE *mSndJump, *mSndDie, *mSndLand, *mSndThrow;

	Vec mVel;
	State mState;
	bool mFlip;
	int mAnim;
	bool mDoubleJump;
	double mJumpGracePeriod;

	WinState mWinState;

	double lastTime;

	Switch *mLeft, *mRight, *mDown, *mUp, *mJump, *mAction;
	Carryable *mCarried;

	void animate(double dt);
	void handleCollisions(double dt);
	double jumpHeight() { return mCarried ? -100.0 : -270.0; };
	void toss();
	void checkWinConditions();

public:
	Avatar();
	~Avatar();

	int *indices()
	{
		static int ind[] = {ID("entt"), ID("rndr"), ID("dyna"), ID("posi"), ID("avat"), ID("prss"), 0};
		return ind;
	}

	int order(int index)
	{
		if (index == ID("rndr"))
			return 40;
		else
			return (int)this;
	}


	void update(double dt);
	void render();
	Rect bbox();

	void dropCarried();

	Entity *clone() { return new Avatar(); }
	const char *code() { return "av"; }
	std::string edString() { return "avatar"; }

	WinState winState() { return mWinState; }
	void die(std::string comment);
	void win();
};

#endif /* AVATAR_H_ */
