bandit

oldschool thievery roguelike
git clone git://moonbender.net/bandit
Log | Files | Refs | README

input.c (1800B)


      1 #include <curses.h>
      2 
      3 #include "common.h"
      4 
      5 extern Thief pc;
      6 extern u32 app_state;
      7 extern Actor actors[MAXACTORS];
      8 
      9 enum {
     10     INPUT_STATE_MOVE,
     11     INPUT_STATE_TARGET,
     12     INPUT_STATE_INTERACT
     13 };
     14 
     15 enum {
     16     LEFT,
     17     RIGHT,
     18     UP,
     19     DOWN
     20 };
     21 
     22 u8 input_state = INPUT_STATE_MOVE;
     23 
     24 void keypress_direction(u8 dir)
     25 {
     26 	u32 xgo = actors[0].x;
     27 	u32 ygo = actors[0].y;
     28 	switch(dir) {
     29 			case LEFT: xgo--;  break;
     30 			case RIGHT: xgo++; break;
     31 			case UP: ygo--; break;
     32 			case DOWN: ygo++; break;
     33 	}
     34 	
     35 	if (input_state == INPUT_STATE_TARGET) {
     36 		bandit_action_interact(xgo, ygo);
     37 		input_state = INPUT_STATE_INTERACT;
     38 		return;
     39 	}
     40 	input_state = INPUT_STATE_MOVE;
     41 	bandit_actor_place(0, xgo, ygo);
     42 	bandit_time_advance();
     43 }
     44 
     45 void curses_input_handle(void) {
     46     int input;
     47 
     48     input = getch();
     49     switch (input) {
     50         case KEY_LEFT:
     51             keypress_direction(LEFT);
     52             break;
     53         case KEY_RIGHT:
     54             keypress_direction(RIGHT);
     55             break;
     56         case KEY_UP:
     57             keypress_direction(UP);
     58             break;
     59         case KEY_DOWN:
     60             keypress_direction(DOWN);
     61             break;
     62         case 'q': /* there are no macros for alphabetical keys. Just treat it like a char and it SHOULD work ?? */
     63             app_state = 0;
     64             break;
     65         case 'i':
     66             input_state = INPUT_STATE_TARGET;
     67             break;
     68         case '1':
     69             if (input_state == INPUT_STATE_INTERACT) {
     70                 bandit_action(1);	
     71             }
     72             break;
     73         case '2':
     74             if (input_state == INPUT_STATE_INTERACT) {
     75                 bandit_action(2);
     76             }
     77             break;
     78         case '3':
     79             if (input_state == INPUT_STATE_INTERACT) {
     80                 bandit_action(3);
     81             }
     82             break;
     83     }
     84 }