//----------------------------------------------------------------------------
// Lesson10: MAKING A GAME
//
// USER INPUT
//
// Without user input, a game is a movie.  So now we are going to actually
// get keypresses, mouse position, and mouse button presses!
//
// In a game, you set up the state of the game, get user input, modify
// stuff according to that input, and then do it all again.  In this example,
// we will move a paddle back and forth, and do a fancy trick to keep the paddle's
// movement limited to the size of the window.
//
// Also, let's move the handier format of one define per bank:entry statement!
// To check out Lesson10...
//
//       MAP TEST
//       LOADAPE Lesson10
//       INVOKE 2:1
//
// This window does not exit, so go to the console and type:
//     ui_closewindow 2:1
//
// That's what ya have to do if your window won't close.  Worst case, if you
// don't even know what the sequence number is:
//     ui_closeallwindows.
//
// NEVER do this from a console command in a window though.  It's bad practice.
//
// Check out Lesson 11 to learn about states!
//
//#############################################################################
// DEFINES
//#############################################################################

#define @UserInput			"2:1"
#define @Before            		"2:2"
#define @During            		"2:3"
#define @After             		"2:4"
#define @GetMouseInput			"2:5"
#define @EnterWasPressed	     	"2:6"
#define @MouseButtonWasPressed  	"2:7"
//-------------------------------------------------------
// CONSTANTS
//-------------------------------------------------------
#define %MSPERFRAME      "50"	// 50 milliseconds * 20 = 1 sec
#define %FRAMESPERSECOND "20"

#define %LEFTSIDE	    	"-100"	// can move 100 left and 100 right
#define %RIGHTSIDE		"100"

#define %PRESSED		"1"	// a button or key was pressed!

//#############################################################################
// CODE
//#############################################################################
#window @UserInput
startswitch @Before
thinkswitch @During
finishswitch @After
//-------------------------------------------------------
width 232
height 256
flags passive  //window can't be clicked away automatically
flags noscroll //text snaps on screen instead of scrolling
title "User Input Wackiness!"

if (enterCounter == 0) body "Enter not pressed.\n"
if (enterCounter > 0) body "Enter was pressed.\n"
if (mouseButtonCounter == 0) body "Mouse Button not pressed.\n"
if (mouseButtonCounter > 0) body "Mouse Button was pressed.\n"

image "zong\bpaddle.pcx" bpaddleX,bpaddleY


//=======================================================
// Before
// Initialize stuff!
//=======================================================
#switch @Before
lastMS = func_GameTime
currentMS = lastMS

bpaddleX = 112			// bottom paddle
bpaddleY = 224


// bind keys and mouse button to our wishes!
console "pushbind"
console "bind enter \"invoke @EnterWasPressed\"; bind mouse1 \"gamevar mouseButtonState 1\""	

console "bind escape \"ui_closewindow 2:1\"" 
// hit escape to quit


return				// at the program

//=======================================================
// During
// Actively update appropriate variables
//=======================================================
#switch @During
set currentMS = func_GameTime		// get starting time, start to count
set delta = currentMS - lastMS
if (delta < %MSPERFRAME) return		// do nothing unless frame starts

set lastMS = currentMS		

// if enter was pressed, show message for 20 seconds
if (enterCounter > 0) enterCounter = enterCounter - 1
if (enterState == %PRESSED) enterCounter = 20		// show message for 1 second
enterState = 0						// (20 frames)

// if Mouse Button 1 was pressed, show message for 20 seconds
if (mouseButtonCounter > 0) mouseButtonCounter = mouseButtonCounter - 1
if (mouseButtonState == %PRESSED) mouseButtonCounter = 20
mouseButtonState = 0

gosub @GetMouseInput
return
//=======================================================
// GetMouseInput
// Move that mouse!
//=======================================================
#switch @GetMouseInput
bpaddleX = (func_mousex * 209) / 640


return

//=======================================================
// After
// Leave the place like we found it
//=======================================================
#switch @After
console "popbind"
unset delta
unset lastMS
unset currentMS
unset bpaddleX 			// bottom paddle
unset bpaddleY 
return

//=======================================================
// EnterWasPressed
// Service Keypad Left being pressed.
//=======================================================
#switch @EnterWasPressed
enterState = %PRESSED
return
//=======================================================
// MouseButtonWasPressed
// Service MouseButton being pressed.
//=======================================================
#switch @MouseButtonWasPressed
mouseButtonState = %PRESSED
return