/*
 *   EZ GL2D Platfom Game engine Example
 *   Source is provided "as is" without express or implied warranty of any kind.
 * 
*/

using System;
using System.Collections.Generic;
using System.Threading;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using EZGL2D;

namespace GL2DexampleJump.Game
{
    public class Player : Entity
    {

        enum DIRECTION
        {
            RIGHT,
            LEFT
        }

        enum STATE
        {
            STANDING,
            RUNNING,
            JUMPING,
        }

        private bool canJump = false;
        private float JUMP_HEIGHT = 9.0f;
        private DIRECTION direction = DIRECTION.RIGHT;
        private STATE state = STATE.STANDING;

        private int ticks = 0;
        
        private int flipmode = GL2D.SpriteGL.FLIP_NONE;

        public Player()
        {
            width = 28;
            height = 38;
            x = Constants.TILE_SIZE * 3;
            y = Constants.TILE_SIZE * 6;
            speed = 2.5f;
            dy = 1;
            canJump = false;

        }

        public void update(LevelMap levelMap, GameWindow gameWindow)
        {

            dx = 0;
            state = STATE.STANDING;
            if (gameWindow.Keyboard[OpenTK.Input.Key.Right])
            {
                dx = speed;
                if(state != STATE.JUMPING) state = STATE.RUNNING;
                direction = DIRECTION.RIGHT;
            }

            if (gameWindow.Keyboard[OpenTK.Input.Key.Left])
            {
                dx = -speed;
                if (state != STATE.JUMPING) state = STATE.RUNNING;
                direction = DIRECTION.LEFT;
            }

            if (gameWindow.Keyboard[OpenTK.Input.Key.Space])
            {
                if (canJump)
                {
                    dy = -JUMP_HEIGHT;
                    canJump = false;
                    state = STATE.JUMPING;
                }
            }


            Collision.CollisionType collisionType = Collision.collideOnMap(this, levelMap);

            if (collisionType == Collision.CollisionType.COLLIDE_FLOOR)
            {
                if (!gameWindow.Keyboard[OpenTK.Input.Key.Space])
                {
                    canJump = true;
                }
            }
            else
            {
                canJump = false;
            }

            tileX = (int)(x / Constants.TILE_SIZE);
            tileY = (int)(y / Constants.TILE_SIZE);


            flipmode = GL2D.SpriteGL.FLIP_NONE;
            if (direction == DIRECTION.LEFT)
            {
                flipmode = GL2D.SpriteGL.FLIP_H;
            }

            ticks++;
            if( (ticks & 3) == 0 )
            {
                frame = (++frame) & 3;
            }
            switch (state)
            {
                case STATE.RUNNING:
                    baseFrame = 0;
                    break;
                case STATE.STANDING:
                    baseFrame = 4;
                    break;
                case STATE.JUMPING:
                    // No animation for jumping so do nothing
                    break;
                
            }
        }

        public void render( GL2D.SpriteGL[] imageAtlas)
        {

            GL2D.Sprite((int)x, (int)y, flipmode, imageAtlas[baseFrame+frame]);
                         
        }

    }

}
