152 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			C
		
	
	
			
		
		
	
	
			152 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			C
		
	
	
| #include "context.h"
 | |
| #include "player.h"
 | |
| #include "game.h"
 | |
| #include "stubs.h" // for interface calls
 | |
| #include "main.h" // for is_running
 | |
| #include "console.h" // for console stuff
 | |
| 
 | |
| void globalContext(int key_press) {
 | |
|   switch(key_press) {
 | |
|     case '`':
 | |
|       if (current_context != &consoleContext) {
 | |
|         current_context = &consoleContext;
 | |
|       } else {
 | |
|         current_context = &walkContext;
 | |
|       }
 | |
|       break;
 | |
|     default:
 | |
|       current_context(key_press);
 | |
|       break;
 | |
|   }
 | |
| }
 | |
| 
 | |
| void consoleContext(int key_press) {
 | |
|   switch(key_press) {
 | |
|     case KEY_ESC:
 | |
|       current_context = &walkContext;
 | |
|       break;
 | |
|     case 13: // enter
 | |
|       // TODO: run command via string here
 | |
|       consoleProcessCommand(console_cmd);
 | |
|       console_cmd_offset = 0;
 | |
|       console_cmd_size = 0;
 | |
|       console_cmd[console_cmd_offset] = '\0';
 | |
|       break;
 | |
|     case 8:
 | |
|     case 127: // delete
 | |
|       console_cmd[console_cmd_offset-1] = '\0';
 | |
|       if (console_cmd_offset > 0)
 | |
|         console_cmd_offset--;
 | |
|         console_cmd_size--;
 | |
|       break;
 | |
|     default:
 | |
|       if (console_cmd_offset < 31) {
 | |
|         console_cmd[console_cmd_offset] = key_press;
 | |
|         console_cmd[console_cmd_offset+1] = '\0';
 | |
|         console_cmd_offset++;
 | |
|         console_cmd_size++;
 | |
|       }
 | |
|       break;
 | |
|   }
 | |
| }
 | |
| 
 | |
| void walkContext(int key_press) {
 | |
|   switch(key_press) {
 | |
|     case 'y':
 | |
|       (*player_commands[PLAYER_MOVE])(NORTHWEST, 1);
 | |
|       break;
 | |
|     case 'u':
 | |
|       (*player_commands[PLAYER_MOVE])(NORTHEAST, 1);
 | |
|       break;
 | |
|     case KEY_UP:
 | |
|     case 'k':
 | |
|       (*player_commands[PLAYER_MOVE])(NORTH, 1);
 | |
|       break;
 | |
|     case KEY_DOWN:
 | |
|     case 'j':
 | |
|       (*player_commands[PLAYER_MOVE])(SOUTH, 1);
 | |
|       break;
 | |
|     case 'b':
 | |
|       (*player_commands[PLAYER_MOVE])(SOUTHWEST, 1);
 | |
|       break;
 | |
|     case 'n':
 | |
|       (*player_commands[PLAYER_MOVE])(SOUTHEAST, 1);
 | |
|       break;
 | |
|     case KEY_RIGHT:
 | |
|     case 'l':
 | |
|       (*player_commands[PLAYER_MOVE])(EAST, 1);
 | |
|       break;
 | |
|     case KEY_LEFT:
 | |
|     case 'h':
 | |
|       (*player_commands[PLAYER_MOVE])(WEST, 1);
 | |
|       break;
 | |
|     case 'a':
 | |
|       current_context = &activateContext;
 | |
|       interfacePrint("Activate what?");
 | |
|       break;
 | |
|     case 'L':
 | |
|       current_context = &lookContext;
 | |
|       interfacePrint("Look where?");
 | |
|       break;
 | |
|   }
 | |
| }
 | |
| 
 | |
| void lookContext(int key_press) {
 | |
|   switch(key_press) {
 | |
|     case KEY_ESC:
 | |
|       interfacePrint("back in action, bb!");
 | |
|       break;
 | |
|     case KEY_UP:
 | |
|     case 'k':
 | |
|       (*player_commands[PLAYER_LOOK])(player->x, player->y-1);
 | |
|       break;
 | |
|     case KEY_DOWN:
 | |
|     case 'j':
 | |
|       (*player_commands[PLAYER_LOOK])(player->x, player->y+1);
 | |
|       break;
 | |
|     case KEY_RIGHT:
 | |
|     case 'l':
 | |
|       (*player_commands[PLAYER_LOOK])(player->x+1, player->y);
 | |
|       break;
 | |
|     case KEY_LEFT:
 | |
|     case 'h':
 | |
|       (*player_commands[PLAYER_LOOK])(player->x-1, player->y);
 | |
|       break;
 | |
|     case '.':
 | |
|       (*player_commands[PLAYER_LOOK])(player->x, player->y);
 | |
|       break;
 | |
|     default:
 | |
|       interfacePrint("invalid direction");
 | |
|       break;
 | |
|   }
 | |
|   current_context = &walkContext;
 | |
| }
 | |
| 
 | |
| void activateContext(int key_press) {
 | |
|   switch(key_press) {
 | |
|     case KEY_ESC:
 | |
|       interfacePrint("back in action, bb!");
 | |
|       break;
 | |
|     case KEY_UP:
 | |
|     case 'k':
 | |
|       (*player_commands[PLAYER_ACTIVATE])(player->x, player->y-1);
 | |
|       break;
 | |
|     case KEY_DOWN:
 | |
|     case 'j':
 | |
|       (*player_commands[PLAYER_ACTIVATE])(player->x, player->y+1);
 | |
|       break;
 | |
|     case KEY_RIGHT:
 | |
|     case 'l':
 | |
|       (*player_commands[PLAYER_ACTIVATE])(player->x+1, player->y);
 | |
|       break;
 | |
|     case KEY_LEFT:
 | |
|     case 'h':
 | |
|       (*player_commands[PLAYER_ACTIVATE])(player->x-1, player->y);
 | |
|       break;
 | |
|     default:
 | |
|       interfacePrint("invalid direction");
 | |
|       break;
 | |
|   }
 | |
|   current_context = &walkContext;
 | |
| }
 |