using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;//www.gorselprogramlama.com
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Char_Jump_Game
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        List<Keys> basilanTuslar = new List<Keys>();

        float startY = 0f;
        float finishY = 0f;
        float jumpDistance = 35f;
        bool jumping = false;
        bool down = false;
       
        void ziplamaAyar()
        {
            startY = pbChar.Top;//www.gorselprogramlama.com
            finishY = pbChar.Top - jumpDistance; // daha yukarısı
            jumping = true;
            down = false;
            tmrZipla.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (jumping)
            {
                if (!down) // up
                {
                    if (pbChar.Top >= finishY)
                    {//www.gorselprogramlama.com
                        pbChar.Top-=2;
                    }
                    else down = true;
                }
                else // down
                {
                    if (pbChar.Top < startY)
                    {                        
                        pbChar.Top += 2;
                        //gravity();
                    }
                    else
                    {
                        jumping = false;
                        tmrZipla.Enabled = false;
                    }
                }
            }//www.gorselprogramlama.com
        }       

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (!basilanTuslar.Contains(e.KeyCode))
            {
                basilanTuslar.Add(e.KeyCode);
            }
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            basilanTuslar.Remove(e.KeyCode);//www.gorselprogramlama.com
        }

        private void tmrTusKontrol_Tick(object sender, EventArgs e)
        {
            lblCharXY.Text = pbChar.Top + "," + pbChar.Left;
            //gravity();
            if (!jumping && basilanTuslar.Contains(Keys.Space))
            {
                ziplamaAyar();                
            }
            else if (basilanTuslar.Contains(Keys.Left))
            {
                if (!jumping && basilanTuslar.Contains(Keys.Space))
                {
                    ziplamaAyar();
                }
                //www.gorselprogramlama.com
                /*if (pbChar.Left > 0)
                {
                    pbChar.Left -= 1;
                }
                */

                if (pbChar.Left <= -pbChar.Width)
                {
                    pbChar.Left = (this.Width - 8);
                }
                pbChar.Left -= 1;

                resimKontrol(Properties.Resources.char_sol);               
            }
            else if (basilanTuslar.Contains(Keys.Right))
            {
                if (!jumping && basilanTuslar.Contains(Keys.Space))
                {//www.gorselprogramlama.com
                    ziplamaAyar();
                }

                /*if (pbChar.Left < (this.Width - pbChar.Width) - 10)
                {
                    pbChar.Left += 1;
                }
                */

                if (pbChar.Left >= (this.Width - 8))
                {
                    pbChar.Left = -pbChar.Width;
                }
                pbChar.Left += 1;

                resimKontrol(Properties.Resources.char_sag);                
            }
        }

        void resimKontrol(Bitmap bmp)
        {
            if (pbChar.Image != bmp)
            {
                pbChar.Image = bmp;//www.gorselprogramlama.com
            }
        }

        void gravity()
        {
            bool fall = false;
            foreach (Control item in this.Controls)
            {
                if (item is Label)
                {
                    if (item.Tag != null && item.Tag.ToString().StartsWith("zemin"))
                    {
                        int charTemas = pbChar.Top + pbChar.Height;
                        int zemin = item.Top;

                        float mutlakX = Math.Abs((pbChar.Left + (pbChar.Width / 2)) - (item.Left + (item.Width / 2)));
                        float mutlakY = Math.Abs((pbChar.Top + (pbChar.Height / 2)) - (item.Top + (item.Height / 2)));
                        float farkGenislik = (pbChar.Width / 2) + (item.Width / 2);
                        float farkYukselik = (pbChar.Height / 2) + (item.Height / 2);

                        if (charTemas - 1 == zemin && (farkGenislik > mutlakX) && (farkYukselik > mutlakY))
                        {
                            fall = true;
                            startY = item.Top - pbChar.Width;
                            break;//www.gorselprogramlama.com
                        }
                        else
                        {
                            fall = false;
                        }
                    }
                }
            }
            if (!fall)
            {
                startY = lblPlace.Top;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
