my code now:
Code: ags
Now they will no longer move upwards, and there still isn't any collision detection. Also, they are no longer lined up with my grid, but that isn't as important as the ability to move upwards.
// global script
int GridUnitWidth = 20; // set this to the width of each section of the grid
int GridUnitHeight = 20; // the height of each section
int GetGridX(int roomX) {
return ((roomX / GridUnitWidth) * GridUnitWidth) + (GridUnitWidth / 2);
}
int GetGridY(int roomY) {
return ((roomY / GridUnitWidth) * GridUnitWidth) + (GridUnitWidth / 2);
}
function ChangeTurn() {
if (game.score == 0) {
player.ChangeView (player.View-1);
if ((player == cRed2) || (player == cBlue2)) {
//Switch Sides
if (player == cRed2) cBlue.SetAsPlayer();
if (player == cBlue2) cRed1.SetAsPlayer();
}
else {
//Switch Players
if (player == cBlue) cBlue2.SetAsPlayer();
if (player == cRed1) cRed2.SetAsPlayer();
}
player.ChangeView (player.View+1);
GiveScore(5);
}
}
void WalkToXYOnGrid(Character *theChar, int roomX, int roomY) {
if (theChar == null) return;
roomX = GetGridX(roomX);
roomY = GetGridY(roomY);
Character *charAtXY = Character.GetAtScreenXY(roomX - GetViewportX(), roomY - GetViewportY());
if (charAtXY != null) {
int xdiff = theChar.x - roomX;
int ydiff = theChar.y - roomY;
if (xdiff < 0) xdiff = -xdiff;
if (ydiff < 0) ydiff = -ydiff;
if (xdiff > ydiff) {
if (theChar.x < charAtXY.x) {
if (roomX > (GridUnitWidth / 2)) roomX -= GridUnitWidth;
else roomX += GridUnitWidth;
}
else {
if (roomX < (Room.Width - (GridUnitWidth / 2))) roomX += GridUnitWidth;
else roomX -= GridUnitWidth;
}
}
else {
if (theChar.y < charAtXY.y) {
if (roomY > (GridUnitHeight / 2)) roomY -= GridUnitHeight;
else roomY += GridUnitHeight;
}
else {
if (roomY < (Room.Height - (GridUnitHeight / 2))) roomY += GridUnitHeight;
else roomY -= GridUnitHeight;
}
}
}
theChar.Walk(roomX, roomY);
}
#sectionstart game_start // DO NOT EDIT OR REMOVE THIS LINE
function game_start() // called when the game starts, before the first room is loaded
{
GiveScore(5);
}
#sectionend game_start // DO NOT EDIT OR REMOVE THIS LINE
#sectionstart repeatedly_execute // DO NOT EDIT OR REMOVE THIS LINE
function repeatedly_execute() {
}
#sectionend repeatedly_execute // DO NOT EDIT OR REMOVE THIS LINE
#sectionstart on_key_press // DO NOT EDIT OR REMOVE THIS LINE
function on_key_press(int keycode) {// called when a key is pressed. keycode holds the key's ASCII code
if (IsGamePaused()==1) keycode=0; // game paused, so don't react to keypresses
if (keycode==17) QuitGame(1); // Ctrl-Q
if (keycode==363) SaveGameDialog(); // F5
if (keycode==365) RestoreGameDialog(); // F7
if (keycode==367) RestartGame(); // F9
if (keycode==434) SaveScreenShot("scrnshot.pcx"); // F12
if (keycode==9) InventoryScreen(); // Tab, show inventory
if (keycode==19) Debug(0,0); // Ctrl-S, give all inventory
if (keycode==22) Debug(1,0); // Ctrl-V, version
if (keycode==1) Debug(2,0); // Ctrl-A, show walkable areas
if (keycode==24) Debug(3,0); // Ctrl-X, teleport to room
while (player.Moving) Wait(1);
if (keycode==375) {
WalkToXYOnGrid(player, player.x - 20, player.y);
GiveScore(-1);
ChangeTurn();
}
if (keycode==377) {
WalkToXYOnGrid(player, player.x+20, player.y);
GiveScore(-1);
ChangeTurn();
}
if (keycode==380) {
WalkToXYOnGrid(player, player.x, player.y+20);
GiveScore(-1);
ChangeTurn();
}
if (keycode==372) {
WalkToXYOnGrid(player, player.x, player.y-20);
GiveScore(-1);
ChangeTurn();
}
}
#sectionend on_key_press // DO NOT EDIT OR REMOVE THIS LINE
#sectionstart on_mouse_click // DO NOT EDIT OR REMOVE THIS LINE
function on_mouse_click(MouseButton button) // called when a mouse button is clicked. button is either LEFT or RIGHT
{
if (IsGamePaused() == 1) // Game is paused, so do nothing (ie. don't allow mouse click)
{
}
else if (button == eMouseLeft)
{
ProcessClick(mouse.x,mouse.y, mouse.Mode);
}
else // right-click, so show menu
{
gPopup.Visible = true;
}
}
#sectionend on_mouse_click // DO NOT EDIT OR REMOVE THIS LINE
#sectionstart interface_click // DO NOT EDIT OR REMOVE THIS LINE
function interface_click(int interface, int button) {
}
#sectionend interface_click // DO NOT EDIT OR REMOVE THIS LINE
#sectionstart Quit_Click
function Quit_Click(GUIControl *control, MouseButton button) {
QuitGame(1);
}
#sectionend Quit_Click
#sectionstart popupclose_Click
function popupclose_Click(GUIControl *control, MouseButton button) {
gPopup.Visible = false;
}
#sectionend popupclose_Click
#sectionstart Help_Click
function Help_Click(GUIControl *control, MouseButton button) {
Display("Arrow keys: Move");
}
#sectionend Help_Click
#sectionstart character1_a // DO NOT EDIT OR REMOVE THIS LINE
function character1_a() {
// script for Character 1 (RED2): Any click on character
}
#sectionend character1_a // DO NOT EDIT OR REMOVE THIS LINE
#sectionstart unhandled_event // DO NOT EDIT OR REMOVE THIS LINE
function unhandled_event(int what, int type) {
}
#sectionend unhandled_event // DO NOT EDIT OR REMOVE THIS LINE
Now they will no longer move upwards, and there still isn't any collision detection. Also, they are no longer lined up with my grid, but that isn't as important as the ability to move upwards.