Creation/Dev/Script/Vector Position Functions: Difference between revisions
From Graal Bible
(Created page with '{{Under Construction|author=Chris Vimes}}') |
No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
{{Under Construction|author=[[User:Cbk1994|Chris Vimes]]}} | {{Under Construction|author=[[User:Cbk1994|Chris Vimes]]}} | ||
{{g|vecx}} and {{g|vecy}} are two commonly-used GScript functions which help calculate positions based on a direction. | |||
vecx(dir) - returns the amount a player would move on the x axis if moving in direction ''dir'' | |||
vecy(dir) - returns the amount a player would move on the y axis if moving in direction ''dir'' | |||
The amount is based on a player speed of one tile per movement. You can multiply the returned value by your desired speed to find the appropriate movement delta for your situation. | |||
== Reference Table == | |||
{| border="1" cellpadding="3" | |||
! Direction | |||
! vecx(dir) | |||
! vecy(dir) | |||
|- | |||
| {{g|0}} (up) | |||
| {{g|0}} | |||
| {{g|-1}} | |||
|- | |||
| {{g|1}} (left) | |||
| {{g|-1}} | |||
| {{g|0}} | |||
|- | |||
| {{g|2}} (down) | |||
| {{g|0}} | |||
| {{g|1}} | |||
|- | |||
| {{g|3}} (right) | |||
| {{g|1}} | |||
| {{g|0}} | |||
|} | |||
== Example (Staff Boots) == | |||
//#CLIENTSIDE | |||
function onCreated() { | |||
this.speed = 1; | |||
} | |||
function onKeyPressed(code, key) { | |||
if (key == "b") { | |||
// toggle boots | |||
this.on = ! this.on; | |||
if (this.on) { | |||
this.trigger("timeout", null); | |||
} | |||
} | |||
} | |||
function onTimeOut() { | |||
if (! this.on) { | |||
return; | |||
} | |||
for (temp.key = 0; key < 4; key ++) { | |||
if (keydown(key)) { // is the key down? | |||
// move the player in the appropriate direction using vecx and vecy, multiplying | |||
// them by the current speed | |||
player.x += (vecx(key) * this.speed); | |||
player.y += (vecy(key) * this.speed); | |||
} | |||
} | |||
this.setTimer(0.05); | |||
} | |||
function onPlayerChats() { | |||
if (player.chat.starts("/speed ")) { | |||
this.speed = player.chat.substring(7).trim(); | |||
} | |||
} | |||
== See Also == | |||
* [http://forums.graalonline.com/forums/showthread.php?p=1649547 Tig's {{g|vecx}} and {{g|vecy}} explanation] | |||
[[Category:Scripting Reference]] |
Latest revision as of 14:34, 24 September 2011
This article is currently being written or rewritten by Chris Vimes.
vecx and vecy are two commonly-used GScript functions which help calculate positions based on a direction.
vecx(dir) - returns the amount a player would move on the x axis if moving in direction dir vecy(dir) - returns the amount a player would move on the y axis if moving in direction dir
The amount is based on a player speed of one tile per movement. You can multiply the returned value by your desired speed to find the appropriate movement delta for your situation.
Reference Table
Direction | vecx(dir) | vecy(dir) |
---|---|---|
0 (up) | 0 | -1 |
1 (left) | -1 | 0 |
2 (down) | 0 | 1 |
3 (right) | 1 | 0 |
Example (Staff Boots)
//#CLIENTSIDE function onCreated() { this.speed = 1; } function onKeyPressed(code, key) { if (key == "b") { // toggle boots this.on = ! this.on; if (this.on) { this.trigger("timeout", null); } } } function onTimeOut() { if (! this.on) { return; } for (temp.key = 0; key < 4; key ++) { if (keydown(key)) { // is the key down? // move the player in the appropriate direction using vecx and vecy, multiplying // them by the current speed player.x += (vecx(key) * this.speed); player.y += (vecy(key) * this.speed); } } this.setTimer(0.05); } function onPlayerChats() { if (player.chat.starts("/speed ")) { this.speed = player.chat.substring(7).trim(); } }