#include <io.h>
#include <system.h>
#include <stdio.h>
#include "vga_update.h"
//------------------------------------------------
  extern int p1_x[2], p1_y[2], p2_x[2], p2_y[2];
  extern int p1_center[2], p2_center[2];
  extern int p1_width[2], p2_width[2];
  extern int p1_image[2], p2_image[2];
  extern int p1_flip[2], p2_flip[2];
  extern int p1_color[2], p2_color[2];
  
  extern int ds;
int vga_cnt = 0;
int second = 0;
int sixth_second = 0;
int minute = 0;

//------------------------------------------------
void sprite_refresh()
{
//close interrupt for this cycle and clear sprites
    IOWR_END_IRQ();
    sprite_clear_all();
    
//update timer variables
    if(vga_cnt < 60)    vga_cnt++;
    else {vga_cnt = 0;    second++; sixth_second++;}
    second = second%60;
    sixth_second = sixth_second%10;
    if(second == 59) minute++;

        //update player body images and phantoms
        if(!p1_flip[ds]){
            IOWR_SET_SPRITE( p1_x[ds] - p1_center[ds], p1_y[ds], p1_image[ds], 4, p1_flip[ds], p1_color[ds]);
        }else{
            IOWR_SET_SPRITE( p1_x[ds] - p1_width[ds] + p1_center[ds], p1_y[ds], p1_image[ds], 4, p1_flip[ds], p1_color[ds] );  
        }
        
        if(!p2_flip[ds]){
            IOWR_SET_SPRITE( p2_x[ds] - p2_center[ds], p2_y[ds], p2_image[ds], 5, p2_flip[ds], p2_color[ds] );
        }else{
            IOWR_SET_SPRITE( p2_x[ds] - p2_width[ds] + p2_center[ds], p2_y[ds], p2_image[ds], 5, p2_flip[ds], p2_color[ds] );
        }

     
}

void fight_environment(int p1_hp_off, int p2_hp_off, int time)
{
    IOWR_SET_BG_COLOR(BACKGROUND); //turn on background image
    IOWR_SET_SPRITE( 12, 10, 34, 15, 1, ORANGE);// update P1 hp boundary
    IOWR_SET_SPRITE( 12, 10, 33, 14, 1, ORANGE);// update P1 hp value   
    IOWR_LEFT_CUT( 16 + p1_hp_off, 14 );         // update current P1 hp off value
    IOWR_SET_SPRITE( 380, 10, 34, 17, 0, ORANGE);// update P2 hp boundary
    IOWR_SET_SPRITE( 380, 10, 33, 16, 0, ORANGE);// update P2 hp value
    IOWR_RIGHT_CUT( 16 + p2_hp_off, 16 );        // update current P2 hp off value
    IOWR_SET_SPRITE( 273, 5, 23+(time/10), 12, 0, ORANGE);//update time second tens
    IOWR_SET_SPRITE( 316, 5, 23+(time%10), 13, 0, ORANGE);//update time second ones
    if (time == 0){
        IOWR_SET_SPRITE( 197, 150, 36, 19, 0, ORANGE);
        if(p1_hp_off > p2_hp_off)   IOWR_SET_SPRITE( 264, 220, 38, 18, 0, ORANGE);
        else    IOWR_SET_SPRITE( 264, 220, 37, 18, 0, ORANGE);
    }// show up winner words when the fight ends
}

void game_menu(int cursor_position)
{
    IOWR_SET_BG_COLOR(BLACK);  
}// show game menu image

void sprite_clear_all()
{
    int i;
    for(i = 0; i<21; i++) IOWR_SET_SPRITE( 1023, 1023, 0, i, 0, 0 );
}//clear all sprite before a new cycle



