Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Glenjamin on Tue 30/01/2018 16:08:01

Title: Move player to mouse coordinates
Post by: Glenjamin on Tue 30/01/2018 16:08:01
Hello everyone,

I wanted to program a unique kind of movement for a part in my game where the player is on a raft.

My plan was to make a region that when clicked on, sets the player to walk to the mouse location, but with some distance added to simulate momentum.

Also it would be blocked so the player could only move to one location, then wait, and then move to another location to simulate the raft only moving when they paddle with an oar

Code (ags) Select
function hswimtest_AnyClick()
{
//if the mouse is in front of the player
if (mouse.x > player.x){
    player.Move(mouse.x+5, mouse.y, eNoBlock, eAnywhere);

//if the mouse is behind the player   
   }else  if (mouse.x < player.x){
     player.Move(mouse.x-5, mouse.y, eBlock, eAnywhere);

   } 
}


The issue is the mouse coordinates seem to change to the far left of the screen, and the character moves there instead of where the mouse had clicked.

As far as I can tell the in-engine walk mechanics aren't interfering with this. Thoughts?
Title: Re: Move player to mouse coordinates
Post by: selmiak on Tue 30/01/2018 16:20:20
is this a scrolling room? Then you have to add the viewport coordinates to the mouse coordinates.
Title: Re: Move player to mouse coordinates
Post by: Glenjamin on Tue 30/01/2018 16:29:28
Quoteis this a scrolling room? Then you have to add the viewport coordinates to the mouse coordinates.

This IS a scrolling room!

How would I go about adding the viewport coordinates to the mouse coordinates?
Title: Re: Move player to mouse coordinates
Post by: Crimson Wizard on Tue 30/01/2018 16:38:40
This is asked every now and then in the tech forums -
http://www.adventuregamestudio.co.uk/forums/index.php?topic=51063.msg636497399#msg636497399
Title: Re: Move player to mouse coordinates
Post by: dayowlron on Tue 30/01/2018 16:38:57

function hswimtest_AnyClick()
{
//if the mouse is in front of the player
if (mouse.x > player.x){
    player.Move(GetViewPortX() + mouse.x+5, GetViewPortY() + mouse.y, eNoBlock, eAnywhere);

//if the mouse is behind the player   
   }else  if (mouse.x < player.x){
     player.Move(GetViewPortX() + mouse.x-5, GetViewPortY() + mouse.y, eBlock, eAnywhere);

   } 
}
Title: Re: Move player to mouse coordinates
Post by: Crimson Wizard on Tue 30/01/2018 16:41:51
dayowlron, you forgot to subtract Viewport coordinate from player.x:
Code (ags) Select

if (mouse.x > player.x - GetViewportX())


IMO its better to translate everything into room coordinates first. This way the further calculations will be much simplier.

Code (ags) Select

int room_click_x = mouse.x + GetViewportX();
int room_click_y = mouse.y + GetViewportY();
if (room_click_x  > player.x){
    player.Move(room_click_x + 5, room_click_y, eNoBlock, eAnywhere);

and so on...

EDIT: my apologies, did wrong conversion :) fixed now.
Title: Re: Move player to mouse coordinates
Post by: dayowlron on Tue 30/01/2018 17:52:12
good point.