0 votes

Hello everyone! I am working through a tutorial, and I was able to get the following movement code from fornclake's Zelda-like Tutorial on YouTube (thank you fornclake!!).

Now I would like to add grid-based movement to this script logic, like a Pokemon clone. Can anyone help me with this? Thank you!!

Here is what I have so far:

extends KinematicBody2D

const SPEED = 70

var movedir = Vector2(0,0)
var spritedir = "down"

func physicsprocess(delta):
controlsloop()
movement
loop()
spritedir_loop()

if movedir != Vector2(0,0):
    anim_switch("walk")
else:
    anim_switch("idle")

func controlsloop():
var LEFT = Input.is
actionpressed("uileft")
var RIGHT = Input.isactionpressed("uiright")
var UP = Input.is
actionpressed("uiup")
var DOWN = Input.isactionpressed("ui_down")

if movedir.y == 0:
    movedir.x = -int(LEFT) + int(RIGHT)
if movedir.x == 0:
    movedir.y = -int(UP) + int(DOWN)

func movementloop():
var motion = movedir.normalized() * SPEED
move
and_slide(motion, Vector2(0,0))

func spritedir_loop():
match movedir:
Vector2(-1,0):
spritedir = "left"
Vector2(1,0):
spritedir = "right"
Vector2(0,-1):
spritedir = "up"
Vector2(0,1):
spritedir = "down"

func animswitch(animation):
var newanim = str(animation,spritedir)
if $anim.current
animation != newanim:
$anim.play(newanim)

in Engine by (12 points)

Please edit your post and format the code for readability. To do that, edit the post, select the code, and click the "{ }" button in the post editor's toolbar.

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.