rewrite this to .gd script instead of c: using Godot; using System; public partial class…
- User
rewrite this to .gd script instead of c:
using Godot; using System;
public partial class Player : CharacterBody3D { public const uint MaxLifepoints = 200;
[Export] public float Speed { get; private set; } = 5.0f; [Export] public float JumpVelocity { get; private set; } = 4.5f; [Export] private uint _lifepoints = MaxLifepoints; // Get the gravity from the project settings to be synced with RigidBody nodes. public float gravity = ProjectSettings.GetSetting("physics/3d/default_gravity").AsSingle(); private GameManager _gameManager; private Timer _attackTimer; private Node3D _visual; private ProgressBar _playerLifebar; private AnimationTree _animationTree; public override void _Ready() { base._Ready(); _gameManager = GetNode<GameManager>("/root/GameManager"); _gameManager.Player = this; _playerLifebar = GetTree().CurrentScene.GetNode<ProgressBar>("HUD/PlayerLifeBar"); _playerLifebar.MaxValue = MaxLifepoints; _playerLifebar.Value = _lifepoints; _attackTimer = GetNode<Timer>("AttackCooldown"); _visual = GetNode<Node3D>("Visual"); _animationTree = GetNode<AnimationTree>("AnimationTree"); } public override void _Process(double delta) { base._Process(delta); _playerLifebar.Value = _lifepoints; } public override void _PhysicsProcess(double delta) { Vector3 velocity = Velocity; // Add the gravity. if (!IsOnFloor()) velocity.Y -= gravity * (float)delta; // 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 inputDir = Input.GetVector("left", "right", "up", "down"); Vector3 direction = (Transform.Basis * new Vector3(inputDir.X, 0, inputDir.Y)).Normalized(); if (direction != Vector3.Zero) { velocity.X = direction.X * Speed; velocity.Z = direction.Z * Speed; } else { velocity.X = Mathf.MoveToward(Velocity.X, 0, Speed); velocity.Z = Mathf.MoveToward(Velocity.Z, 0, Speed); } Velocity = velocity; MoveAndSlide(); velocity.Y = 0; _animationTree.Set("parameters/walking/blend_amount", velocity.LimitLength(1).Length()); if (Mathf.IsZeroApprox(velocity.Length())) return; _visual.LookAt(Position + 10 * velocity, Vector3.Up, true); //var angle = 0; //var transform = _visual.Transform; //transform.Basis = new(Vector3.Up, angle); //_visual.Transform = transform; } public void OnAttackTimeOut() { _attackTimer.Start(); var nearestEnemy = _gameManager.GetNearestEnemy(); if (nearestEnemy == null) return; //nearestEnemy.TakeDamages(); } public void TakeDamages(uint damages) { _lifepoints -= Math.Clamp(damages, 0, _lifepoints); } internal void Heal(uint damages) { _lifepoints = Math.Clamp(_lifepoints + damages, 0, MaxLifepoints); }}
- Assistant
Below is the converted version of the provided C# code into GDScript. Note that this conversion assumes you have the equivalent Godot nodes and setups, as GDScript uses nodes and their paths directly:
extends KinematicBody3D const MAX_LIFEPOINTS := 200 export(float) var speed: float = 5.0 export(float) var jump_velocity: float = 4.5 export var lifepoints: int = MAX_LIFEPOINTS setget set_lifepoints var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity") var game_manager var attack_timer var visual var player_lifebar var animation_tree func _ready(): game_manager = get_node("/root/GameManager") game_manager.player = self player_lifebar = get_tree().current_scene.get_node("HUD/PlayerLifeBar") player_lifebar.max_value = MAX_LIFEPOINTS player_lifebar.value = lifepoints attack_timer = get_node("AttackCooldown") visual = get_node("Visual") animation_tree = get_node("AnimationTree") func set_lifepoints(value): lifepoints = clamp(value, 0, MAX_LIFEPOINTS) player_lifebar.value = lifepoints func _process(delta): player_lifebar.value = lifepoints func _physics_process(delta): var velocity = get_linear_velocity() if not is_on_floor(): velocity.y -= gravity * delta var input_dir = Vector2( Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left"), Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up") ).normalized() var direction: Vector3 = global_transform.basis.xform(Vector3(input_dir.x, 0, input_dir.y)).normalized() if direction != Vector3.ZERO: velocity.x = direction.x * speed velocity.z = direction.z * speed else: velocity.x = lerp(velocity.x, 0, delta * speed) velocity.z = lerp(velocity.z, 0, delta * speed) velocity = move_and_slide(velocity, Vector3.UP) velocity.y = 0 animation_tree.set("parameters/walking/blend_amount", velocity.clamped(1).length()) if velocity.length_squared() > 0.01: visual.look_at(global_transform.origin + velocity, Vector3.UP) func on_attack_timeout() -> void: attack_timer.start() var nearest_enemy = game_manager.get_nearest_enemy() if nearest_enemy == null: return # nearest_enemy.take_damages() func take_damages(damage: int) -> void: lifepoints = clamp(lifepoints - damage, 0, MAX_LIFEPOINTS) func heal(amount: int) -> void: lifepoints = clamp(lifepoints + amount, 0, MAX_LIFEPOINTS)Please ensure you replace the
"ui_left","ui_right","ui_up","ui_down"with your actual action names defined in the Input Map in Godot, and double-check for the correct node paths according to your scene structure.Additionally, as GDScript is a bit more forgiving than C# when it comes to types and doesn't require explicit getter/setter methods, I've provided a simple
set_lifepointsfunction for thelifepointsproperty. You will need to implement any missing functionality such asget_nearest_enemyortake_damageson the enemy within your GDScript project context as necessary.