I have provided the point-n-click movement system I am using on Noctorious. Now, you may be thinking "Yes, this has been done" but I wanted to provide a version that wasn't as complex as Mallard's system for those who haven't the skill or knowledge to hack his system.
This system moves the player at a static speed, rather than taking
acceleration into account, thus acting as an intermediate solution between classic static-speed arrowkey movement and Mallard's point-n-click movement system.
Not only that, but I'd say it'd be slightly easier to learn from this system if you're new-ish to the GScript language.
The collision detection
isn't the greatest, however, I've managed to get the player pretty flush against walls.
Credits:
Mallard - Creator of the system which gave me inspiration and guidance in constructing this system.
PHP Code:
/*
Script: Simple Point-n-Click Movement
Author: Codein
Description:
A point-n-click movement system that moved the player in a static speed.
*/
//#CLIENTSIDE
function onCreated() {
disableDefMovement();
this.moveSpeed = 12/16;
setTimer(0.05);
}
/*
Main Loop.
*/
function onTimeout() {
updateVisuals();
if (leftmousebutton) {
//Set the position in which to move to.
this.position = new TStaticVar();
this.position.x = mousex;
this.position.y = mousey;
updateUnitVectors();
this.moving = true;
}
/*
Check if player is not moving in one direction.
This is because the unit vectors would stay the same,
causing the player to miss the target position, otherwise.
*/
if (player.x == this.playerX || player.y == this.playerY) {
updateUnitVectors();
}
this.playerX = player.x;
this.playerY = player.y;
/*
Check whether the player has reached the target
*/
this.distance = ((this.position.x - player.x) ^ 2 + (this.position.y - player.y) ^ 2) ^ 0.5;
if (this.moving == true && this.distance > 2) {
movePlayer();
}
else {
this.moving = false;
}
setDirection();
setAnimation();
setTimer(0.05);
}
/*
Display a crosshair that turns red when over a blocking tile
and green when you're able to move there.
*/
function updateVisuals() {
temp.aimCross = showtext(201, mousex + 0.5, mousey - 1, "b", "Arial", "+");
temp.aimCross.zoom = 1.5;
if (onwall(mousex, mousey)) {
temp.aimCross.red = 255;
temp.aimCross.blue = 0;
temp.aimCross.green = 0;
}
elseif (leftmousebutton) {
temp.aimCross.red = 0;
temp.aimCross.blue = 0;
temp.aimCross.green = 255;
}
else {
temp.aimCross.blue = 255;
temp.aimCross.green = 255;
temp.aimCross.red = 255;
}
}
/*
Set the unit vectors, which we will use to move the player
in the x/y axis, with the appropriate magnitude,
to reach the target.
*/
function updateUnitVectors() {
this.displacement = new TStaticVar();
this.displacement.x = this.position.x - player.x;
this.displacement.y = this.position.y - player.y;
this.unitVectors = vectornormalize({this.displacement.x, this.displacement.y, 0});
}
/*
Move the player on the x/y axis.
We check and move the player on the x/y axis independently, so that
you can slide along a wall.
*/
function movePlayer() {
//X axis wall check
for (i = 0; i <= this.moveSpeed; i += 1/16) {
temp.xCheck = player.x + this.unitVectors[0] * i;
if (onwall2(temp.xCheck + 0.4, player.y + 2 - 1/16, 2.2, 1)) {
break;
}
}
//X axis movement
player.x += this.unitVectors[0] * (i - (i < this.moveSpeed ? 1/16 : 0));
//Y Axis wall check
for (i = 0; i < this.moveSpeed; i += 1/16) {
temp.yCheck = player.y + this.unitVectors[1] * i;
if (onwall2(player.x + 1, temp.yCheck + 2 - 1/16, 1.6, 1)) {
break;
}
}
//Y axis movement
player.y += this.unitVectors[1] * (i - (i < this.moveSpeed ? 1/16 : 0));
}
function setDirection() {
player.dir = getdir(this.displacement.x, this.displacement.y);
}
/*
Set the gani of the player, depending on whether they are
- Swimming
- Walking
- Idle
*/
function setAnimation() {
if (onwater(player.x + 1.5, player.y + 2)) {
if (player.ani != "swim") setAni("swim", null);
}
elseif (this.moving) {
if (player.ani != "walk") setAni("walk", null);
}
else {
setAni("idle", null);
}
}