keyboard movement module

Started by monkey424, Sat 04/05/2013 15:33:46

Previous topic - Next topic

monkey424

Greetings!

I made some keyboard movement code for my game a while ago and wanted to rehash it for another game.

However I didn't bother to check that there was already a module for this by Khris:

http://www.adventuregamestudio.co.uk/forums/index.php?topic=42843.0

For what it's worth, my code was:

Code: AGS
// main global script file

bool K[4];                  // K[0]=left; K[1]=right; K[2]=up; K[3]=down; 
int  A[2];                  // A[0]=old; A[1]=new;  0 not moving , 1-4 four directions , 5-8 combination of directions

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

// put anything you want to happen every game cycle in here
function repeatedly_execute()
{
  
  // -- DIRECTION KEYS ---- DIRECTION KEYS ---- DIRECTION KEYS --
  
  if (IsGamePaused() == 0) { 
    
    if (IsKeyPressed(eKeyLeftArrow)   == 1)   K[0] = 1;    else K[0] = 0;
    if (IsKeyPressed(eKeyRightArrow)  == 1)   K[1] = 1;    else K[1] = 0;
    if (IsKeyPressed(eKeyUpArrow)     == 1)   K[2] = 1;    else K[2] = 0;
    if (IsKeyPressed(eKeyDownArrow)   == 1)   K[3] = 1;    else K[3] = 0;

    // -- No key pressed -- 
    if (K[0]+K[1]+K[2]+K[3] == 0) {

      if (A[1] != 0) {
        Chaz.StopMoving();
        A[1] = 0;
      }
      
    }
    
    // -- One key pressed --
    else if (K[0]+K[1]+K[2]+K[3] == 1) {
      
      if      (K[0] == 1) A[1] = 1;
      else if (K[1] == 1) A[1] = 2;
      else if (K[2] == 1) A[1] = 3;
      else if (K[3] == 1) A[1] = 4;
      
    }
    
    // -- Two keys pressed --
    else if (K[0]+K[1]+K[2]+K[3] == 2) {
      
      if      ((K[0] == 1) & (K[2] == 1)) A[1] = 5;
      else if ((K[1] == 1) & (K[2] == 1)) A[1] = 6;
      else if ((K[0] == 1) & (K[3] == 1)) A[1] = 7;
      else if ((K[1] == 1) & (K[3] == 1)) A[1] = 8;
      else {
        Chaz.StopMoving();
        A[1] = 0;
      }
      
    }
      
    // -- More than two keys pressed --
    else {
      Chaz.StopMoving();
      A[1] = 0;      
    }
    
    // -- Walk in the direction picked --
    if (A[0] != A[1]) {

      A[0] = A[1];
    
      if      (A[1] == 1) Chaz.WalkStraight(0, Chaz.y, eNoBlock);                   // LEFT
      else if (A[1] == 2) Chaz.WalkStraight(Room.Width, Chaz.y, eNoBlock);          // RIGHT
      else if (A[1] == 3) Chaz.WalkStraight(Chaz.x, 0, eNoBlock);                   // UP
      else if (A[1] == 4) Chaz.WalkStraight(Chaz.x, Room.Height, eNoBlock);         // DOWN
      else if (A[1] == 5) Chaz.WalkStraight(0, Chaz.y - Chaz.x, eNoBlock);                            // LEFT-UP
      else if (A[1] == 6) Chaz.WalkStraight(Room.Width, Chaz.y + Chaz.x - Room.Width, eNoBlock);      // RIGHT-UP
      else if (A[1] == 7) Chaz.WalkStraight(0, Chaz.y + Chaz.x, eNoBlock);                            // LEFT-DOWN
      else if (A[1] == 8) Chaz.WalkStraight(Room.Width, Chaz.y - Chaz.x + Room.Width, eNoBlock);      // RIGHT-DOWN
      
    }
  
  }

}


I would actually like to see an update to the keyboard movement module to deal with the case when the character ('Chaz' in the above example) gets stuck when there is no immediate walkable area beyond the linear path designated upon key press (e.g. maybe there's only a few pixels discontinuing the path, but a slight detour around those missing pixles is possible).

It's not really urgent. I might look at doing this myself later down the track if no one else is interested.

Khris? Your thoughts? Feel free to borrow my code if you like.
    

SMF spam blocked by CleanTalk