268 lines
5.2 KiB
C
Raw Normal View History

2022-04-07 18:46:57 +02:00
/*
* @file: input.c
* @author C. Hölzl
* @brief: Input Handler file
* 2019
*/
#include "input.h"
#include "network.h"
#include "graphics_engine.h"
#include "utils.h"
#define IS_PRESSED(key_code) (keys & key_code)
void handle_keys_solo(game_t* game, gs_t* status)
{
scanKeys();
u32 keys = keysDown();
if (IS_PRESSED(KEY_START)) {
*status = QUIT;
}
if (IS_PRESSED(KEY_SELECT)) {
//HANDLE SELECT
}
if (IS_PRESSED(KEY_A)) {
apply_action_pos(game->maze, game->player, INTERACT_PICKUP, status);
}
if (IS_PRESSED(KEY_B)) {
apply_action_pos(game->maze, game->player, INTERACT_DROP, status);
}
if (IS_PRESSED(KEY_X)) {
apply_action_pos(game->maze, game->player, INTERACT_EXIT, status);
}
if (IS_PRESSED(KEY_Y)) {
apply_action_pos(game->maze, game->player, TAUNT, status);
}
//keys = keysHeld();
if (IS_PRESSED(KEY_RIGHT)) {
apply_action_pos(game->maze, game->player, RIGHT, status);
}
if (IS_PRESSED(KEY_LEFT)) {
apply_action_pos(game->maze, game->player, LEFT, status);
}
if (IS_PRESSED(KEY_UP)) {
apply_action_pos(game->maze, game->player, UP, status);
}
if (IS_PRESSED(KEY_DOWN)) {
apply_action_pos(game->maze, game->player, DOWN, status);
}
if (IS_PRESSED(KEY_R)) {
game->player->pos.r = MOD(game->player->pos.r + 45, 360);
}
if (IS_PRESSED(KEY_L)) {
game->player->pos.r = MOD(game->player->pos.r - 45, 360);
}
}
void handle_keys_multi(gs_t* status)
{
scanKeys();
u32 keys = keysDown();
char action = 0;
if (IS_PRESSED(KEY_START)) {
action = 'Q';
*status = QUIT;
}
if (IS_PRESSED(KEY_SELECT)) {
action = 'F';
}
if (IS_PRESSED(KEY_A)) {
action = 'I';
}
if (IS_PRESSED(KEY_B)) {
action = 'I';
}
if (IS_PRESSED(KEY_X)) {
action = 'I';
}
if (IS_PRESSED(KEY_Y)) {
action = 'T';
}
if (IS_PRESSED(KEY_RIGHT)) {
action = 'D';
}
if (IS_PRESSED(KEY_LEFT)) {
action = 'A';
}
if (IS_PRESSED(KEY_UP)) {
action = 'W';
}
if (IS_PRESSED(KEY_DOWN)) {
action = 'S';
}
if (IS_PRESSED(KEY_L) && IS_PRESSED(KEY_R)) {
action = 'K';
}
if (action != 0 && (*status == RUN || *status == QUIT)) {
net_snd(&action, sizeof(char));
}
}
void handle_keys_menu(gs_t* gs, gt_t* gt)
{
scanKeys();
u32 keys = keysDown();
if (IS_PRESSED(KEY_TOUCH)) {
touchPosition touch;
touchRead(&touch);
if (touch.px > 52 && touch.px < SCREEN_WIDTH - 52 && touch.py > 50 && touch.py < 82) {
*gt = SOLO;
*gs = QUIT;
} else if (touch.px > 52 && touch.px < SCREEN_WIDTH - 52 && touch.py > 100 && touch.py < 132) {
*gt = MULTI;
*gs = QUIT;
} else if (touch.px > 5 && touch.px < 46 && touch.py > 168 && touch.py < 186) {
*gt = GUIDE;
*gs = QUIT;
} else if (touch.px > 196 && touch.px < 250 && touch.py > 168 && touch.py < 186) {
*gt = CREDITS;
*gs = QUIT;
} else if (touch.px > 96 && touch.px < 158 && touch.py > 158 && touch.py < 174) {
*gt = RANKING;
*gs = QUIT;
}
}
if (IS_PRESSED(KEY_START)) {
POWERDWN();
}
}
void handle_keys_submenu(gs_t* gs, gt_t* gt)
{
scanKeys();
u32 keys = keysDown();
if (IS_PRESSED(KEY_START) || IS_PRESSED(KEY_TOUCH)) {
*gs = QUIT;
}
}
void handle_keys_start(gs_t* gs)
{
scanKeys();
u32 keys = keysDown();
if (IS_PRESSED(KEY_START) || IS_PRESSED(KEY_TOUCH)) {
*gs = QUIT;
}
}
int timer_ticks;
void load_timer_ISR()
{
++timer_ticks;
if (timer_ticks % 16 == 0) { //2s
next_loading_isr(0);
}
if (timer_ticks >= 42) {
irqDisable(IRQ_TIMER0);
timer_ticks = 0;
}
}
u32 game_time = 0;
void game_time_ISR()
{
game_time = MIN(game_time + 1, SECTIME_MAX_VALUE);
}
u32 get_game_time()
{
return game_time;
}
void reset_game_time()
{
game_time = 0;
}
void init_load_timer()
{
timer_ticks = 0;
irqEnable(IRQ_TIMER0);
TIMER_DATA(0) = TIMER_FREQ_1024(8);
TIMER0_CR = TIMER_ENABLE | TIMER_DIV_1024;// | TIMER_IRQ_REQ;
irqSet(IRQ_TIMER0, &load_timer_ISR);
TIMER0_CR |= TIMER_IRQ_REQ;
}
void init_game_timer()
{
game_time = 0;
irqEnable(IRQ_TIMER1);
TIMER_DATA(1) = TIMER_FREQ_1024(1);
TIMER1_CR = TIMER_ENABLE | TIMER_DIV_1024;// | TIMER_IRQ_REQ;
irqSet(IRQ_TIMER1, &game_time_ISR);
TIMER1_CR |= TIMER_IRQ_REQ;
}
void disable_game_timer()
{
game_time = 0;
irqDisable(IRQ_TIMER1);
}
void init_audio()
{
mmInitDefaultMem((mm_addr)soundbank_bin);
mmLoad(MOD_FROZEN_26);
//mmLoadEffect(SFX_SWISH);
//mmLoadEffect(SFX_CLUNK);
mmStart(MOD_FROZEN_26, MM_PLAY_LOOP);
mmSetModuleVolume(512);
}
void play_sfx_audio(int i)
{
mm_sound_effect sound;
sound.id = i;
sound.rate = 1024;
sound.volume = 255;
// if(i == SFX_SWISH)
// sound.panning = 0;
// if(i == SFX_CLUNK)
// sound.panning = 255;
mmEffectEx(&sound);
}