#include <string.h>
#include <piece.h>
#include "scsh.h"

unsigned char vbuff[128*88];
sBALL ball_data;
sPLAYER player_data;
int downKey;

void pceAppInit( void )
{
    pceLCDDispStop();
    pceLCDSetBuffer( vbuff );
    pceAppSetProcPeriod( 120 );
    memset( vbuff, 0, 128*88 );
    pceLCDDispStart();
    
    //↓↓ここからソース↓↓
    downKey = 2;
    sBALL_INIT();       //ボール情報の初期化
    sPLAYER_INIT();     //プレーヤー情報の初期化
    screen_create();    //スクリーン作成
    pceLCDTrans();
}

void pceAppProc( int cnt )
{
    int lKey = pcePadGet();
    
    //プレーヤー入力処理
    if (lKey&PAD_LF) {
        downKey++;
        if (player_data.x-15-downKey>=0) player_data.x-=downKey;
        else player_data.x = 15;
    }
    else if(lKey&PAD_RI) {
        downKey++;
        if (player_data.x+15+downKey<=127) player_data.x+=downKey;
        else player_data.x = 112;
    }
    else downKey=2;
    
    //ボール処理
    ball_data.x+=ball_data.move_x;
    ball_data.y+=ball_data.move_y;
    
    
    //壁当て処理
    if (ball_data.x<=0) {
        ball_data.x = 0;
        ball_data.move_x = -ball_data.move_x;
        
        if (ball_data.move_x-1>0) ball_data.move_x--;
    }
    else if(ball_data.x>=122) {
        ball_data.x = 122;
        ball_data.move_x = -ball_data.move_x;
        
        if (ball_data.move_y-1>0) ball_data.move_y--;
    }
    if (ball_data.y<=0) {
        ball_data.y = 0;
        ball_data.move_y = -ball_data.move_y;
        
        if (ball_data.move_x-1>0) ball_data.move_x--;
        if (ball_data.move_y-1>0) ball_data.move_y--;
    }
    else if(ball_data.y>=87) {
        //ゲームオーバー
        sBALL_INIT();       //ボール情報の初期化
        sPLAYER_INIT();     //プレーヤー情報の初期化
        screen_create();    //スクリーン作成
        pceLCDTrans();
    } 
    
    
    //バッティング処理
    if (ball_data.x>=player_data.x-15 || ball_data.x+10>=player_data.x-15)
    if (ball_data.x<=player_data.x+15 || ball_data.x+10<=player_data.x+15) {
        if (ball_data.y+10>=player_data.y) {
            ball_data.y = player_data.y-10;
            ball_data.move_x += (int)((downKey-2)/2);
            ball_data.move_y += (int)((downKey-2)/2);
            ball_data.move_y = -ball_data.move_y;
        }
    }
    
    
    //画面の再描写
    screen_create();
    pceLCDTrans();

}

void pceAppExit( void )
{
}