読者です 読者をやめる 読者になる 読者になる

ハートに16連射。(14)


シューティングモードです。
以前にも近いものをいくつか書いてたので
ストップウォッチモードよりかは簡単と踏んでたのですが多少悩んでしまったり。
デバッグプリントが printf なのはエスケープシーケンスを利用する為です。


ビルドには下記ソース以外に、
「ハートに16連射。(12)」に記した全てのモジュールが必要だったり…。


ハートに16連射。(12)
http://d.hatena.ne.jp/dumbo001/20100201/p1


stgMode module test


main.c

/*---------------------------------------------------------------------------------
	
	shtwatch
	stgMode module test
	
	version 0.01
	Feb 02, 2010
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <string.h>
#include <nds/ndstypes.h>
#include <nds/interrupts.h>
#include <nds/system.h>
#include <nds/arm9/input.h>
#include "Bg.h"
#include "Spr.h"
#include "State.h"
#include "StgMode.h"


typedef enum{
	MAIN_INIT, 
	MAIN_RESET, 
	MAIN_EXEC
}MAIN_ACT;

typedef struct{
	u16 act;
}MainStatus;

static MainStatus st_main;


void main_Init(void);
void main_Reset(void);
bool main_IsReset(void);


//---------------------------------------------------------------------------------
int main(void){
//---------------------------------------------------------------------------------
	
	lcdSwap();
	
	while(1) {
		swiWaitForVBlank();
		scanKeys();
		
		Spr_Update();
		
		if(main_IsReset()){
			st_main.act = MAIN_RESET;
		}
		
		switch(st_main.act){
			
		case MAIN_INIT:
			main_Init();
			st_main.act = MAIN_RESET;
			break;
			
		case MAIN_RESET:
			main_Reset();
			st_main.act = MAIN_EXEC;
			break;
			
		case MAIN_EXEC:
			StgMode_Update();
			
			if(StgMode_IsExit()){
				return 0;
			}
			break;
			
		}
		
	}
	
}

//---------------------------------------------------------------------------------
void main_Init(void){
//---------------------------------------------------------------------------------
	
	memset(&st_main, 0, sizeof(st_main));
	Bg_Init();
	Spr_Init();
	State_Init();
	
	Bg_DrawConsString(0, 0, "stgMode module test\n\nversion 0.01\nFeb 02, 2010\n\nBy. REGEKATSU\n\n\n");
	
}

//---------------------------------------------------------------------------------
void main_Reset(void){
//---------------------------------------------------------------------------------
	
	StgMode_Clear();
	
}

//---------------------------------------------------------------------------------
bool main_IsReset(void){
//---------------------------------------------------------------------------------
	
	bool ans = false;
	
	u32 keys;
	
	if(keysDown() & (KEY_L | KEY_R | KEY_SELECT | KEY_START)){
		
		keys = keysDown() | keysHeld();
		keys = keys & (KEY_L | KEY_R | KEY_SELECT | KEY_START);
		
		if(keys == (KEY_L | KEY_R | KEY_SELECT | KEY_START)){
			ans = true;
		}
		
	}
	
	return ans;
	
}

//---------------------------------------------------------------------------------


stgMode.h

/*---------------------------------------------------------------------------------
	
	shtwatch
	stgMode module header
	
	version 0.01
	Feb 02, 2010
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#ifndef _STG_MODE_H_
#define _STG_MODE_H_


#ifdef __cplusplus
extern "C" {
#endif


void StgMode_Clear(void);
void StgMode_Update(void);
bool StgMode_IsExit(void);


#ifdef __cplusplus
}
#endif

#endif	//_STG_MODE_H_


stgMode.c

/*---------------------------------------------------------------------------------
	
	shtwatch
	stgMode module routine
	
	version 0.01
	Feb 02, 2010
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <stdio.h>//debug print
#include <string.h>
#include <nds/ndstypes.h>
#include <nds/arm9/input.h>
#include "StgMode.h"
#include "State.h"
#include "Spr.h"


#define SCORE_MAX 9999

#define END_TIME 10
#define SET_TIME(x) ((x)*60)


typedef enum{
	STG_MODE_INIT, 
	STG_MODE_BEGIN, 
	STG_MODE_READY, 
	STG_MODE_EXEC, 
	STG_MODE_END, 
	STG_MODE_EXIT
}STG_MODE_ACT;

typedef struct{
	
	u16 act;
	
	u32 frame;
	int time;
	
	int score;
	bool update_high_score;
	
}StgModeStatus;

static StgModeStatus st_stg_mode;


void stgMode_Init(void);
void stgMode_Begin(void);
void stgMode_Ready(void);
void stgMode_Exec(void);
void stgMode_End(void);
void stgMode_Exit(void);
void stgMode_SetReady(void);
void stgMode_ExecTime(void);
void stgMode_ShowTime(int time);
void stgMode_HideTime(void);
void stgMode_ExecScore(void);
void stgMode_ShowScore(int score);
void stgMode_HideScore(void);


//---------------------------------------------------------------------------------
void StgMode_Clear(void){
//---------------------------------------------------------------------------------
	
	stgMode_Exit();
	
}

//---------------------------------------------------------------------------------
void StgMode_Update(void){
//---------------------------------------------------------------------------------
	
	//debug print
	printf("\x01b[22;0H diceMode = %01d, slotMode = %01d", State_GetDiceMode(), State_GetSlotMode());
	
	switch(st_stg_mode.act){
		
	case STG_MODE_INIT:
		stgMode_Init();
		break;
		
	case STG_MODE_BEGIN:
		stgMode_Begin();
		break;
		
	case STG_MODE_READY:
		stgMode_Ready();
		break;
		
	case STG_MODE_EXEC:
		stgMode_Exec();
		break;
		
	case STG_MODE_END:
		stgMode_End();
		break;
		
	case STG_MODE_EXIT:
		stgMode_Exit();
		break;
		
	}
	
}

//---------------------------------------------------------------------------------
void stgMode_Init(void){
//---------------------------------------------------------------------------------
	
	memset(&st_stg_mode, 0, sizeof(st_stg_mode));
	
	Spr_SetColon(0);
	Spr_SetCursor(BIT(0));
	
	stgMode_HideTime();
	stgMode_ShowScore(State_GetHighScore());
	
	st_stg_mode.act = STG_MODE_BEGIN;
	
}

//---------------------------------------------------------------------------------
void stgMode_Begin(void){
//---------------------------------------------------------------------------------
	
	if( (keysDown() & KEY_START) && ( (keysCurrent() & ~KEY_START) == 0) ){
		stgMode_SetReady();
		st_stg_mode.act = STG_MODE_READY;
	}
	
	
	if( (keysDown() & KEY_SELECT) && ( (keysCurrent() & ~KEY_SELECT) == 0) ){
		st_stg_mode.act = STG_MODE_EXIT;
	}
	
}

//---------------------------------------------------------------------------------
void stgMode_Ready(void){
//---------------------------------------------------------------------------------
	
	if( ( ( (keysDown() & KEY_A) && !(keysCurrent() & KEY_B) ) ||
		( (keysDown() & KEY_B) && !(keysCurrent() & KEY_A) ) )
		&& ( (keysCurrent() & ~(KEY_A | KEY_B)) == 0) )
	{
		
		stgMode_ExecScore();
		st_stg_mode.frame--;
		
		st_stg_mode.act = STG_MODE_EXEC;
		
	}
	
	
	if( (keysDown() & KEY_SELECT) && ( (keysCurrent() & ~KEY_SELECT) == 0) ){
		st_stg_mode.act = STG_MODE_EXIT;
	}
	
}

//---------------------------------------------------------------------------------
void stgMode_Exec(void){
//---------------------------------------------------------------------------------
	
	if( ( ( (keysDown() & KEY_A) && !(keysCurrent() & KEY_B) ) ||
		( (keysDown() & KEY_B) && !(keysCurrent() & KEY_A) ) )
		&& ( (keysCurrent() & ~(KEY_A | KEY_B)) == 0) )
	{
		
		stgMode_ExecScore();
		
	}
	
	
	if(st_stg_mode.frame % SET_TIME(1) == 0){
		
		stgMode_ExecTime();
		
	}
	
	
	if(st_stg_mode.frame != 0){
		
		st_stg_mode.frame--;
		
	}else{
		
		//diceMode on
		if(st_stg_mode.score == 62){
			State_SetDiceMode(true);
		}
		//slotMode on
		else if(st_stg_mode.score == 160){
			State_SetSlotMode(true);
		}
		
		if(st_stg_mode.score > State_GetHighScore()){
			State_SetHighScore(st_stg_mode.score);
			st_stg_mode.score = 0;
			st_stg_mode.update_high_score = true;
		}
		
		st_stg_mode.act = STG_MODE_END;
		
	}
	
}

//---------------------------------------------------------------------------------
void stgMode_End(void){
//---------------------------------------------------------------------------------
	
	if(st_stg_mode.update_high_score == true){
		
		if(st_stg_mode.frame == 0){
			stgMode_HideScore();
		}else if(st_stg_mode.frame == 30){
			stgMode_ShowScore( State_GetHighScore() );
		}
		
		if(st_stg_mode.frame < 59){
			st_stg_mode.frame++;
		}else{
			st_stg_mode.frame = 0;
		}
		
	}
	
	
	if( (keysDown() & KEY_START) && ( (keysCurrent() & ~KEY_START) == 0) ){
		stgMode_SetReady();
		st_stg_mode.act = STG_MODE_READY;
	}
	
	
	if( (keysDown() & KEY_SELECT) && ( (keysCurrent() & ~KEY_SELECT) == 0) ){
		st_stg_mode.act = STG_MODE_EXIT;
	}
	
}

//---------------------------------------------------------------------------------
void stgMode_Exit(void){
//---------------------------------------------------------------------------------
	
	Spr_Clear();
	st_stg_mode.act = STG_MODE_INIT;
	
}

//---------------------------------------------------------------------------------
void stgMode_SetReady(void){
//---------------------------------------------------------------------------------
	
	st_stg_mode.update_high_score = false;
	
	st_stg_mode.time = END_TIME;
	st_stg_mode.frame = SET_TIME(END_TIME);
	
	stgMode_ShowTime(st_stg_mode.time);
	stgMode_ShowScore(State_GetHighScore());
	
}

//---------------------------------------------------------------------------------
void stgMode_ExecTime(void){
//---------------------------------------------------------------------------------
	
	st_stg_mode.time--;
	stgMode_ShowTime(st_stg_mode.time);
	
	
}

//---------------------------------------------------------------------------------
void stgMode_ShowTime(int time){
//---------------------------------------------------------------------------------
	
	printf("\x01b[9;5H%2d", time);//debug print
	
	Spr_SetNumber(SEG_SECOND, time, false);
	
}

//---------------------------------------------------------------------------------
void stgMode_HideTime(void){
//---------------------------------------------------------------------------------
	
	printf("\x01b[9;5H  ");//debug print
	
	Spr_SetSegment(0, 0, SEG_SECOND);
	
}

//---------------------------------------------------------------------------------
void stgMode_ExecScore(void){
//---------------------------------------------------------------------------------
	
	if(st_stg_mode.score < SCORE_MAX){
		st_stg_mode.score++;
	}
	
	stgMode_ShowScore(st_stg_mode.score);
	
}

//---------------------------------------------------------------------------------
void stgMode_ShowScore(int score){
//---------------------------------------------------------------------------------
	
	printf("\x01b[9;0H%4d", score);//debug print
	
	if(score == 0){
		
		Spr_SetNumber(SEG_MINUTE, score, true);
		Spr_SetNumber(SEG_HOUR, score, false);
		
	}else if(score < 100){
		
		Spr_SetNumber(SEG_MINUTE, score, false);
		Spr_SetSegment(0, 0, SEG_HOUR);
		
	}else if(score <= SCORE_MAX){
		
		Spr_SetNumber(SEG_MINUTE, score % 100, true);
		Spr_SetNumber(SEG_HOUR, score / 100, false);
		
	}
	
}

//---------------------------------------------------------------------------------
void stgMode_HideScore(void){
//---------------------------------------------------------------------------------
	
	printf("\x01b[9;0H    ");//debug print
	
	Spr_SetSegment(0, 0, SEG_MINUTE);
	Spr_SetSegment(0, 0, SEG_HOUR);
	
}


//---------------------------------------------------------------------------------
bool StgMode_IsExit(void){
//---------------------------------------------------------------------------------
	
	return (st_stg_mode.act == STG_MODE_INIT) ? true:false;
	
}

//---------------------------------------------------------------------------------