
Labyrinth of the Minotaur A simple roguelike/RPG using a nokia 5110 screen
Dependencies: N5110 PowerControl mbed
Diff: Game.cpp
- Revision:
- 31:5b4a4d225ab4
- Parent:
- 30:4a03611a3d99
- Child:
- 32:99ca304085e6
--- a/Game.cpp Sat May 09 11:46:32 2015 +0000 +++ b/Game.cpp Sat May 09 17:37:40 2015 +0000 @@ -16,11 +16,46 @@ { StartFlag = 1; } +/* void DirPressed() { DirFlag = 1; } +*/ +//Joystick functions +// read default positions of the joystick to calibrate later readings +void calibrateJoystick() +{ + //button.mode(PullDown); + // must not move during calibration + joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) + joystick.y0 = yPot; +} +void updateJoystick() +{ + // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) + joystick.x = xPot - joystick.x0; + joystick.y = yPot - joystick.y0; + // read button state + //joystick.button = button; + + // calculate direction depending on x,y values + // tolerance allows a little lee-way in case joystick not exactly in the stated direction + if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { + joystick.direction = Centre; + } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { + joystick.direction = Up; + } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { + joystick.direction = Down; + } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { + joystick.direction = Right; + } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { + joystick.direction = Left; + } else { + joystick.direction = Unknown; + } +} //High score system @@ -171,18 +206,14 @@ Sleep(); } - if (DirFlag) { - - DirFlag = 0; - - if(Up) { - if(menu > 0) { - menu--; - } - } else if(Down) { - if(menu < 3) { - menu++; - } + if (joystick.direction == Up) { + if(menu > 0) { + menu--; + } + } + if(joystick.direction == Down) { + if(menu < 3) { + menu++; } } if (ActFlag) { @@ -217,9 +248,8 @@ } lcd.refresh(); - if (DirFlag) { - - DirFlag = 0; + + if (joystick.direction != Centre) { bright = !bright; } @@ -262,15 +292,14 @@ //RevealMap(); //Uncomment for debugging to view map - while (1) { +srand(Noise * 1000000); - srand(Noise * 1000000); +PlayerCamera(); + + while (1) { ActFlag = 0; StartFlag = 0; - DirFlag = 0; - - PlayerCamera(); if (map[px][py] == CHEST) { Chest(); @@ -290,9 +319,7 @@ Sleep(); - if (DirFlag) { - - DirFlag = 0; + if (joystick.direction != Centre) { PlayerMove(); @@ -300,6 +327,9 @@ if (ph < PH_MAX) { ph++; } + + PlayerCamera(); + } if (rand() % 50 == 0) { @@ -425,7 +455,6 @@ while (1) { ActFlag = 0; StartFlag = 0; - DirFlag = 0; lcd.clear(); @@ -449,8 +478,7 @@ lcd.refresh(); Sleep(); - if (DirFlag) { - DirFlag = 0; + if (joystick.direction != Centre) { menu = !menu; } if (ActFlag) { @@ -647,11 +675,11 @@ Sleep(); } - if (DirFlag) { - DirFlag = 0; - if ((Down || Right) && menu < 3) { + if (joystick.direction != Centre) { + + if ((joystick.direction == Down || joystick.direction == Right) && menu < 3) { menu++; - } else if ((Up || Left) && menu > 0) + } else if ((joystick.direction == Up || joystick.direction == Left) && menu > 0) menu--; } if (ActFlag) { @@ -681,7 +709,6 @@ } StartFlag = 0; ActFlag = 0; - DirFlag = 0; } void DrawMap() @@ -725,10 +752,6 @@ if (ActFlag) { ActFlag = 0; } - if (DirFlag) { - DirFlag = 0; - break; - } if (StartFlag) { StartFlag = 0; break; @@ -758,10 +781,6 @@ if (ActFlag) { ActFlag = 0; } - if (DirFlag) { - DirFlag = 0; - break; - } if (StartFlag) { StartFlag = 0; break; @@ -794,8 +813,6 @@ int r = rand() % 10; - DirFlag = 0; - lcd.clear(); lcd.printString("Do you want to", 0, 1); lcd.printString("take the", 0, 2); @@ -827,8 +844,7 @@ } else { StartFlag = 0; - DirFlag = 0; - + lcd.clear(); lcd.printString("You leave the", 0, 1); lcd.printString(ItemList[r].ItemName, 0, 2); @@ -971,7 +987,6 @@ Sleep(); } } else { //Leave the potion - DirFlag = 0; StartFlag = 0; lcd.clear(); lcd.printString("You walk away", 0, 0); @@ -1046,16 +1061,21 @@ int main() { + calibrateJoystick(); // get centred values of joystick + pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second + //Power Saving PHY_PowerDown(); int result = semihost_powerdown(); - Up.rise(&DirPressed); - Down.rise(&DirPressed); - Right.rise(&DirPressed); - Left.rise(&DirPressed); - Start.rise(&StartPressed); - Act.rise(&ActPressed); + Start.mode(PullDown); + Act.mode(PullDown); + + Start.fall(&StartPressed); + Act.fall(&ActPressed); + + Start.mode(PullDown); + Act.mode(PullDown); //Generate random seed srand(Noise * 1000000);