import java.awt.*;
import java.applet.*;
import box;

public class shot extends Applet implements Runnable
{
    double test;
    Graphics GR;
    Thread ourThread;
    int mx, wait, nr;
    Image Host1;
    Image Host2;
    Image Host3;
    Image Friend;
    Image Background;
    Image Bullet;
    Image Ammo;
    Image NewAmmo;
    target Guy[] = new target[4];
    picture pic[] = new picture[4];
    int status, count;
    public TextField display;
    public TextField counter;
    int account;
    MediaTracker tracker;
    Panel Show;
    int Difficulty;
    AudioClip Hit;
    AudioClip Reload;
    AudioClip NoAmmo;
    box msgBox;
    String ButtonName, Boxtitle;
    String Title, Author, Update;

    public void start()
    {
        if (ourThread == null)
        {
            ourThread = new Thread(this);
            ourThread.start ();
        }
    }
    public void stop ()
    {
        if (ourThread != null)
        {
            ourThread.stop();
            ourThread = null;
        }
    }


    public void init()
    {
        GR= getGraphics();
        account=5;
        status=0;
        count=0;
        Difficulty = 1200;
        resize(400,400);
        Host1 = getImage (getDocumentBase (),"crusher.jpg");
        Host2 = getImage (getDocumentBase (), "new.jpg");
        Host3 = getImage (getDocumentBase (), "worf.jpg");
        Friend = getImage (getDocumentBase (), "homerf.gif");
        Bullet = getImage (getDocumentBase (), "bullet.jpg");
        Background = getImage (getDocumentBase (), "house.jpg");
        Ammo = getImage(getDocumentBase(), "ammo.jpg");
        NewAmmo = getImage(getDocumentBase(), "reload.jpg");

        Hit = getAudioClip(getDocumentBase(), "hit.au");
        Reload = getAudioClip(getDocumentBase(), "reload.au");
        NoAmmo = getAudioClip(getDocumentBase(), "empty.au");

        Title = getParameter("Title");
        Author = getParameter("Author");
        Update = getParameter("Update");
        Boxtitle = ("Information about " + Title);

        display = new TextField(status + " Hits!", 10);
        display.setEditable(false);
        display.setBackground(Color.white);
        counter = new TextField(count + " Shots!", 10);
        counter.setEditable(false);
        counter.setBackground(Color.white);
        Choice diff = new Choice();
        diff.addItem("Chicken");
        diff.addItem("Human");
        diff.addItem("God");

        Show = new Panel();
        Show.add(new Button("New Game"));
        Show.add(display);
        Show.add(counter);
        Show.add(diff);
        Show.add(new Button("About FATE"));
        Show.setBackground(Color.white);
        setLayout(new BorderLayout());
        add("South", Show);

        pic[0] = new picture(Host1,100,62);
        pic[1] = new picture(Host2,80,96);
        pic[2] = new picture(Host3,100,86);
        pic[3] = new picture(Friend,100,79);

        ammo shot[] = new ammo[6];
        int n = 0;

        for (int i=1; i<91;)
        {
            shot[n]= new ammo(i);
            i = i + 15;
            n++;
        }

        tracker = new MediaTracker(this);
        tracker.addImage(Background, 0);
        tracker.addImage(Host1, 1);
        tracker.addImage(Host2, 2);
        tracker.addImage(Host3, 3);
        tracker.addImage(Friend, 4);
        tracker.addImage(Bullet, 5);
        tracker.addImage(Ammo, 6);
        tracker.addImage(NewAmmo, 7);

        showStatus("Loading images. Please wait.");
        try
        {
            tracker.waitForAll();
        }
        catch(InterruptedException e)
        {
            return;
        }
    }


public void run()
{
    showStatus("Have fun while playing FATE!!");

    target Guy[] = new target[4];
    Guy[0] = new target(200,10);
    Guy[1] = new target(150,170);
    Guy[2] = new target(10,100);
    Guy[3] = new target(300,200);

    GR= getGraphics();
    for(;;)
    {
        mx = (int)(Math.random()*10/3);
        wait =(int)(Math.random()*1000/2);
        nr = (int)(Math.random()*10/3);
        GR.drawImage(pic[nr].getPic(),Guy[mx].getX_L(), Guy[mx].getY_O(), this);

            try
            {
                Thread.sleep (Difficulty);
            }
            catch (InterruptedException except)
            {
            }

            repaint(Guy[mx].getX_L(), Guy[mx].getY_O(),pic[mx].getW()+40,pic[mx].getH()+20);

            try
            {
                Thread.sleep (wait);
            }
            catch (InterruptedException except)
            {
            }
    }
}

    public void paint(Graphics g)
    {
        setBackground(Color.white);
        g.setColor(Color.white);
        g.fillRect(0,0,400,400);
        g.setColor(Color.black);

        g.drawImage(Background, 1,1,this);

        ammo shot[] = new ammo[6];
        int n = 0;

        for (int i=1; i<91;)
        {
            shot[n]= new ammo(i);
            i = i + 15;
            n++;
        }

        for (int j=0; j<6; j++)
        {
            g.drawImage(Ammo, shot[j].getAmmoX(),305,this);
        }

        g.drawImage(NewAmmo, 182,320, this);

    }

 public boolean action(Event e, Object o)
    {
        if(o.equals("About FATE"))
        {
            msgBox = new box(this, Title, Author, Update);
            msgBox.setBackground(Color.white);
            msgBox.setTitle(Boxtitle);
            msgBox.resize(300,200);
            msgBox.show();
        }

        if(o.equals("New Game"))
        {
            account=5;
            status=0;
            count=0;
            counter.setText(count + " Shots!");
            display.setText(status + " Hits!");
            repaint();
        }

        if(e.target instanceof Choice)
        {
            if("Chicken".equals(o))
            {
                Difficulty = 1200;
                return true;
            }

            if("Human".equals(o))
            {
                Difficulty = 900;
                return true;
            }

            if("God".equals(o))
            {
                Difficulty = 600;
                return true;
            }
        }
        return false;

    }

 public boolean mouseDown(Event e, int x_pos, int y_pos)
    {
        ammo shot[] = new ammo[6];
        int n = 0;

        for (int i=1; i<91;)
        {
            shot[n]= new ammo(i);
            i = i + 15;
            n++;
        }

        Guy[0] = new target(200,10);
        Guy[1] = new target(150,170);
        Guy[2] = new target(10,100);
        Guy[3] = new target(300,200);

        if (x_pos > 182 && x_pos < 214 && y_pos > 320 && y_pos < 352)
        {
            account = 5;
            for (int j=0; j<6; j++)
                {
                 GR.drawImage(Ammo, shot[j].getAmmoX(),305,this);
                }
            Reload.play();
            return true;
        }

        if (account >= 0 && y_pos < 300)
        {
            Hit.play();
            GR.drawImage(Bullet, x_pos, y_pos, this);
            count ++;
            counter.setText(count + " Shots!");

            GR.clearRect(shot[account].getAmmoX(), 305, 90,30);
            account --;

            if ((x_pos >= Guy[mx].getX_L())
            && (x_pos < (Guy[mx].getX_L() + pic[nr].getW()))
            && (y_pos > Guy[mx].getY_O())
            && (y_pos < (Guy[mx].getY_O() + pic[nr].getW())))
            {
                if (nr != 3)
                {
                    status ++;
                }
                else
                {
                    status --;
                }

                GR.drawImage(Bullet, x_pos, y_pos, this);
                display.setText(status + " Hits!");
                try
                {
                    Thread.sleep (Difficulty);
                }
                catch (InterruptedException except)
                {
                }

                repaint(x_pos, y_pos, 5,5);
            }

        }

        if (account < 0)
        {
            NoAmmo.play();
        }

        return true;
    }




}

class target
{
    public target(int x1, int y1)
    {
        x_l = x1;
        y_o = y1;
    }

    public int getX_L()
    {
        return x_l;
    }

    public int getY_O()
    {
        return y_o;
    }

    private int x_l;
    private int y_o;
}


class picture
{
    public picture(Image Pic, int h, int w)
    {
        Picture = Pic;
        Height = h;
        Width = w;
    }

    public Image getPic()
    {
        return Picture;
    }

    public int getH()
    {
        return Height;
    }

    public int getW()
    {
        return Width;
    }

    private Image Picture;
    private int Height;
    private int Width;
}

class ammo
{
    public ammo(int x)
    {
        x_pos = x;
    }

    public int getAmmoX()
    {
        return x_pos;
    }

    private int x_pos;
}
