view schedule.c @ 0:0fae5658fb0b

Initial revision
author gongo
date Thu, 02 Nov 2006 08:55:19 +0000
parents
children b6a1385f19be
line wrap: on
line source

/*
 * 2006/07/24 16:26:00 sugiyama
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libps2.h"
#include "ps2util.h"
#include "col.h"
#include "field.h"
#include "car.h"
#include "gSprite.h"
#include "gFont.h"
#include "game.h"
#include "controler.h"
#include "camera.h"
#include "mytype.h"
#include "light.h"
#include "title_scene.h"
#include "game_time.h"


#define MAXCAR   3 // 選択可能機体
#define MAXFIELD 2 // 選択可能コース
#define DEMO_NUM 2.0 // デモの数
#define MAXRAP   3 // ラップ数
#define SP_SEL_CAR    50
#define SP_SEL_COURSE 80
	extern int car_check();
/* --- controler.c --- */
extern SGO_PAD pad;
/* --- car.c --- */
extern CarPtr car_init();
extern void car_accelerate(CarPtr, int);
extern void car_swerve(CarPtr, int);
extern void car_update(CarPtr);
/* --- carNode.c */
extern void carNode_append(CarPtr);
extern void carNode_draw();
extern void carNode_destroy();
/* --- field.c --- */
extern void field_init();
extern void field_update(CarPtr);
extern void field_destroy();
/* --- mytype.c --- */
extern void wait_init();
extern Bool wait(double);
extern void time_RaceStart();
extern double time_RaceTime();
/* --- demo.c --- */
extern void demo_openFp(int);
extern void demo_closeFp();
/* --- linda.c --- */
extern void linda_jikiInfo_init();


static int ranking = 0;
static LIGHT l;

static int i=0;



static int
get_random()
{
  srand((unsigned)time(NULL));
  return (int)(rand()*DEMO_NUM/(1.0+RAND_MAX));
}

static void
graphic_Init()
{
  gSprite_Init(); // グラフィック関連の初期化
  gFont_Init();   // フォント関連の初期化
}

static void
play_init()
{
    game.jiki = car_init(game.car_id);
    carNode_append(game.jiki);
    field_init();
}

static void
syokika()
{
    game.car_id      = 1;
    game.course_id   = 1;
    game.camera_type = 0;
    game.select      = 0;
    game.demo_flg    = 0;
    game.rap         = 1;
    game.jiki        = NULL;

    ranking = 0;

    wait_init();
    linda_jikiInfo_init();
}


void
schedule()
{
  char raptime[10];
  
  /* timer */
  static int start_time,time_count;
  static int RUNNIG=0;
  

    switch (game.state) {
    case GAME_INIT:
      	graphic_Init();
	syokika();
	camera_init();

#ifdef DEMO_CREATE
	game.car_id    = 1;
	game.course_id = 2;
	demo_openFp(2);
	play_init();
	game.state = GAME_MAIN;
#else
#ifdef LINDA
	game.state = GAME_WAIT;
#else
	game.state = GAME_OPENING;
#endif
#endif
	break;
    case GAME_WAIT:
	if (pad.st == 1) {
	    game.state = GAME_OPENING;
	}
	break;
    case GAME_OPENING:
      /*title*/
             {
      	 static int blink_count = 0;
    	 /* 点滅 */
	 if (game.play_id==1){
      	 if (blink_count < 35) {
      	   gFont_SetString("PUSH START !!", 170, 380);
      	 }
      	 blink_count = (blink_count > 70) ? 0 : blink_count+1;
	 }
	     }

#ifndef LINDA
	/* 時間が経てばデモ画面へ移行 */
      	if (wait(1.0) == TRUE) {
	  demo_openFp(get_random());
	  game.demo_flg = 1;
	  title_finish_call();
	  game.state = GAME_MAIN_INIT;
      	}
#endif
	if (game.play_id==1){
	if (title_scene() < 0){
	game.state = GAME_SELECT_CAR;
	}
	}else if (game.play_id==2){
	  if (i==0){
	    title_init_call();
	    i=1;
	  }
	}
      
	break;
    case GAME_SELECT_CAR:
      if (i==1){
	title_finish_call();
	i=2;
      }
      gSprite_PutSpriteEx(SP_SEL_CAR+game.car_id, 190, 200, 1.5, 1.5);
      gSprite_PutSprite(24, 460, 300);
      gSprite_PutSprite(25, 120, 300);
      gFont_SetString("SELECT CAR", 180, 50);

      if (pad.right == 1) {
	game.car_id =
	  (game.car_id > MAXCAR-1) ? 1 : game.car_id + 1;
      }
      if (pad.left == 1) {
	    game.car_id =
	      (game.car_id < 2) ? MAXCAR : game.car_id - 1;
      }
      if (pad.circle == 1) {
	game.state = GAME_SELECT_COURSE;
      }
      break;
    case GAME_SELECT_COURSE:
      gSprite_PutSpriteEx(SP_SEL_COURSE+game.course_id, 190, 200, 1.7, 1.8);
      gSprite_PutSprite(24, 460, 300);
      gSprite_PutSprite(25, 120, 300);
      
      gFont_SetString("SELECT COURSE", 150, 50);
      
      if (pad.right == 1) {
	game.course_id =
	  (game.course_id > MAXFIELD-1) ? 1 : game.course_id + 1;
      }
      if (pad.left == 1) {
	game.course_id =
	  (game.course_id < 2) ? MAXFIELD : game.course_id - 1;
	}
      if (pad.circle == 1) {
	game.state = GAME_READY;
      }
      break;
    case GAME_READY:
      gSprite_PutSprite(27+game.play_id,265,10);
      gSprite_PutSpriteEx(SP_SEL_CAR+game.car_id, 50, 120, 1.5, 1.5);
      gSprite_PutSpriteEx(SP_SEL_COURSE+game.course_id, 340, 120, 1.7, 1.8);
      {
	static int blink_count = 0;
	
	/* 点滅 */
	if (blink_count < 35) {
	  gFont_SetString("GAME START !!", 170, 380);
	}
	blink_count = (blink_count > 70) ? 0 : blink_count+1;
      }
      
      if (pad.st > 0) {
	game.state = GAME_MAIN_INIT;
      }
      if (pad.cross > 0) {
	game.state = GAME_SELECT_CAR;
      }
      break;
    case GAME_MAIN_INIT:
      if (!game.jiki)
	play_init();
      RUNNIG=0;
      game.state = GAME_MAIN;
      break;
    case GAME_MAIN:

    /** begin: dispaly RAP TIME **/
      if(RUNNIG==0){
	 start_time = game_time_get_msec();
         RUNNIG=1;
      }

      time_count = game_time_get_msec() - start_time;
      game_time_set_raptime(raptime,time_count);
      gFont_SetString("TIME",300,20);
      gFont_SetString(raptime,400,20);
    /** end: dispaly RAP TIME **/


      
      light_init(&l);
      set_light(&l);
     
      car_update(game.jiki);
      //      check_car();
      field_update(game.jiki);
      camera_update(game.jiki->body->transfer);
      carNode_draw();
      
      gFont_SetStringInt(game.rap, 50, 100);

      /* スピードメーター */
      gSprite_DefSprite(23, 1, 1,(int)(106.0*(game.jiki->speed/game.jiki->speed_max)), 34);
      gSprite_PutSprite(23, 400, 400);
      /* km/h */
      gSprite_PutSprite(26, 470, 350);
      /* Rap */
      gSprite_PutSprite(27, 80, 100);

      if (game.demo_flg == 1) {
	gFont_SetString("D E M O", 280, 200);
      }

      if (game.rap > MAXRAP) {
	wait_init();
	ranking = 1;
	game.state = GAME_GOAL;
      }

      gFont_SetStringInt((int)(100.0*game.jiki->speed), 380, 350);
      
      if ( (pad.right > 0) && ( (game.jiki->speed != 0) || (pad.circle > 0) )) 
	{
	car_swerve(game.jiki, 1);
	}
	if ( (pad.left > 0) && ( (game.jiki->speed !=0) || (pad.circle > 0) ) )
	  {
	    car_swerve(game.jiki, -1);
	  }
	if (pad.circle > 0) {
	  car_accelerate(game.jiki, 1);
	}
	if (pad.cross > 0) {
	  car_accelerate(game.jiki, -1);
	}
	if (pad.r1 == 1) {
	  game.camera_type = !game.camera_type;
	}
	if (pad.st == 1) {
	  if (game.demo_flg == 1) {
	    game.state = GAME_FINISH;
	  } else {
	    game.state = GAME_PAUSE;
	  }
	}
	break;
    case GAME_PAUSE:
      field_update(game.jiki);
      carNode_draw();
      
      if (game.play_id == 1) {
	/* 選択マーク */
	gSprite_PutSprite(17, 100, 190+game.select*100);
	
	gFont_SetString("BACK TO GAME", 200, 200);
	gFont_SetString("GO TO TITLE",  200, 300);
      } else {
	gFont_SetString("Pause ...", 200, 200);
      }

      if (pad.circle == 1) {
	if (game.select == 0) {
	  game.state = GAME_MAIN;
	} else {
	  game.state = GAME_FINISH;
	}
      }
      if (pad.st == 1) {
	game.state = GAME_MAIN;
      }
      if (pad.up == 1 || pad.down == 1) {
	game.select = !game.select;
      }
      break;
    case GAME_GOAL:

      /** dispaly TOTAL TIME **/ 
      game_time_set_raptime(raptime,time_count);

      gFont_SetString("TOTAL TIME",150,20);
      gFont_SetString(raptime,400,20);


      
      gFont_SetString("GOAL !!", 220, 150);
#ifdef LINDA
      if (ranking == 1)
	gFont_SetString("You WIN!!", 200, 250);
      else
	gFont_SetString("You Lose...", 200, 250);
#endif
      car_update(game.jiki);
      field_update(game.jiki);
      carNode_draw();
      camera_update(game.jiki->body->transfer);
      if (pad.st == 1) {
	game.state = GAME_FINISH;
      }
      break;
    case GAME_FINISH:
      if (game.demo_flg == 1) {
	game.state = GAME_OPENING;
      } else {
	gFont_SetString("GAME OVER ...", 200, 200);
      }
      if (game.jiki) {
	field_destroy();
	carNode_destroy();
	demo_closeFp();
	syokika();
      }
      if (pad.st == 1) {
	game.state = GAME_OPENING;
      }
      break;
    default:
      game.state = GAME_OPENING;
      break;
    }
    
    wait_sync();
    swap_dbuff();
    sjoy_poll();
    
    ps2util_sprite_Draw();
    gSprite_Draw_Reset();
    gFont_Draw_Reset();
}