Indholdsfortegnelse

Player movement

I denne tutorial vil du lære at:

Ressourcer

Nedenstående spritesheet indeholder Player animation idle, walk, attack (venstre/højre, op og ned), samt death of player.

Vælg:
Project → Project settings → General → Rendering → Textures
Sæt her 'Default texture filter' til 'nearest'.


I en 2D top-down verden ønsker vi et Object der kan detektere kollisioner, men ikke er påvirket af fysiske kræfter som tyngde (eller andre krafter). Til dette formål bruger vi et


Keyboard Events

For at kunne flytte Player, vil vi opfange nogle Keyboard events (A,S,D,W), og flytte position på Player ud fra disse.

using Godot;
using System;
 
public partial class movement : CharacterBody2D
{
	public const float Speed = 20.0f;
	public const float JumpVelocity = -400.0f;
 
	// Get the gravity from the project settings to be synced with RigidBody nodes.
	public float gravity = ProjectSettings.GetSetting("physics/2d/default_gravity").AsSingle();
 
	public override void _PhysicsProcess(double delta)
	{
		Vector2 velocity = Velocity;
 
		// Add the gravity.
 
		// Handle Jump.
		if (Input.IsActionJustPressed("ui_accept") && IsOnFloor())
			velocity.Y = JumpVelocity;
 
		// Get the input direction and handle the movement/deceleration.
		// As good practice, you should replace UI actions with custom gameplay actions.
		Vector2 direction = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down");
		if (direction != Vector2.Zero)
		{
			velocity.X = direction.X * Speed;
		}
		else
		{
			velocity.X = Mathf.MoveToward(Velocity.X, 0, Speed);
		}
		Velocity = velocity;
		//MoveAndSlide();
		MoveAndCollide(Velocity);
	}
 
 
}

using Godot;
using System;
 
public partial class PlayerMovement : CharacterBody2D
{
	public const float speed = 60.0f;
	public string current_dir = "none";
	private Vector2 temp;
	AnimatedSprite2D anim;
 
	public override void _Ready()
	{
		GD.Print("Printing from Player");
		anim =  GetNode<AnimatedSprite2D>("AnimatedSprite2D");
		anim.Play("idle_front");
	}
 
	public override void _PhysicsProcess(double delta)
	{
		PlayerMovement2(delta);
	}
 
	public void PlayerMovement2(double delta)
	{
		if ( Input.IsKeyPressed(Key.D)){
			current_dir = "right";
			PlayAnimation(1);
			temp = new Vector2(speed,0);
			Velocity = temp;
 
		}
		else if( Input.IsKeyPressed(Key.A)){
			current_dir = "left";
			PlayAnimation(1);
 
			temp = new Vector2(-speed,0);
			Velocity = temp;
		}
		else if( Input.IsKeyPressed(Key.W)) {
			current_dir = "up";
			PlayAnimation(1);
			temp = new Vector2(0, -speed);
			Velocity = temp;
		}
		else if( Input.IsKeyPressed(Key.S)) {
			current_dir = "down";
			PlayAnimation(1);
			temp = new Vector2(0, speed);
			Velocity = temp;
		}
		else if( Input.IsKeyPressed(Key.V)) {
			//DialogManager dm = GetNode<DialogManager>("/root/DialogManager");
			//DialogManager.LoadDialogue("wizard.json");
			//dm.ShowDialogue();
		}
		else if( Input.IsKeyPressed(Key.B)) {
			//DialogManager dm = GetNode<DialogManager>("/root/DialogManager");
			//dm.HideDialogue();
		}
		else {
			PlayAnimation(0);
			Velocity = new Vector2(0,0);
		}
 
		MoveAndSlide();
	}
 
	void PlayAnimation(int movement){
 
		string dir = current_dir;
 
		if ( dir == "right" ) {
			anim.FlipH = false;
			if (movement == 1) {
				anim.Play("walk_right");
			}
			else if ( movement == 0 ){
				anim.Play("idle_right");
			}
		}
		else if ( dir == "left") {
			anim.FlipH = true;
			if (movement == 1)
				anim.Play("walk_right");
			else if (movement == 0)
				anim.Play("idle_right");
		}
		else if (dir == "up"){
			if ( movement == 1)
				anim.Play("walk_up");
			else if (movement == 0)
				anim.Play("idle_up");
		}
		else if (dir == "down"){
			if (movement == 1)
				anim.Play("walk_down");
			else if (movement == 0)
				anim.Play("idle_front");
		}
	}
	}

Animation

Shift to Run

Tilknytning af en movement modifier ved at holde Shift tasten nede, feks ved RUN vs WALK.

(WIP)