more efficient ray cast position code

This commit is contained in:
2022-12-11 21:44:18 +01:00
parent c186097d0e
commit 0a90c41a23

View File

@@ -3,10 +3,9 @@ using System;
public partial class player : CharacterBody2D public partial class player : CharacterBody2D
{ {
[Export] [Export] public string playerName;
public string playerName; [Export] public int speed = 400;
[Export] [Export] public int rayCastLength = 64;
public int speed = 400;
public Vector2 movement; public Vector2 movement;
public AnimatedSprite2D animatedSprite; public AnimatedSprite2D animatedSprite;
@@ -27,16 +26,13 @@ public partial class player : CharacterBody2D
public override void _Process(double delta) public override void _Process(double delta)
{ {
//set ray_cast target position //set ray_cast target position
int raylength = 64; Vector2 rayCastPosition = new Vector2((float)Math.Round(movement.x), (float)Math.Round(movement.y)) * rayCastLength;
if (Input.IsActionJustPressed("move_right")) GetNode<RayCast2D>("ray_cast_2d").TargetPosition = new Vector2(raylength, 0); if (rayCastPosition.Length() != 0) GetNode<RayCast2D>("ray_cast_2d").TargetPosition = rayCastPosition;
if (Input.IsActionJustPressed("move_left")) GetNode<RayCast2D>("ray_cast_2d").TargetPosition = new Vector2(-raylength, 0);
if (Input.IsActionJustPressed("move_down")) GetNode<RayCast2D>("ray_cast_2d").TargetPosition = new Vector2(0, raylength);
if (Input.IsActionJustPressed("move_up")) GetNode<RayCast2D>("ray_cast_2d").TargetPosition = new Vector2(0, -raylength);
//call event in raycasted object //call event in raycasted object
if (Input.IsActionJustPressed("ui_accept") && GetNode<RayCast2D>("ray_cast_2d").IsColliding()) if (Input.IsActionJustPressed("ui_accept") && GetNode<RayCast2D>("ray_cast_2d").IsColliding())
GetNode<RayCast2D>("ray_cast_2d").GetCollider().Call("OnInteraction", playerName); GetNode<RayCast2D>("ray_cast_2d").GetCollider().Call("OnInteraction", playerName);
//animation system (with controller support wcih cant get normalized vector) //animation system (with controller support wcih cant get normalized vector)
if (movement.Length() != 0) if (movement.Length() != 0)
animatedSprite.Play(); animatedSprite.Play();
else else
{ {