rewrite this so I can control it when I swap to 3d plane:
extends CharacterBody3D
const MAX_LIFEPOINTS := 200
export var speed := 5.0
export var jump_velocity := 4.5
var _lifepoints: int = MAX_LIFEPOINTS setget , get_lifepoints
var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity")
var _attackTimer: Timer
var _visual: Node3D
var _playerLifebar: ProgressBar
var _animationTree: AnimationTree
func _on_SwapPlane():
var sceneChangerScript = load("res://Scripts/Global/SceneChanger.gd") as Script
var sceneChangerNode = get_node("SceneChanger")
sceneChangerNode.call("change_to_2d")
my 2d plane:
extends CharacterBody2D
class_name Player2D
enum State { IDLE, RUN, JUMP, ATTACK }
var state = State.IDLE
var bullets_amount: int = 30
var attack_animation: String = "Attack1"
var player_base = PlayerBase.new()# Use PlayerBase’s set_health method¨
@export var movement_data : MovementData
@export var stats : Stats
References
@onready var animator : AnimatedSprite2D = AnimatedSprite2D@onreadyvargunsanimator:AnimationPlayer=ShootingAnimationPlayer
@onready var hit_animator : AnimationPlayer = HitAnimationPlayer@onreadyvarhand:NHand
@onready var pistol : Sprite2D = Hand/Pivot/Pistol@onreadyvaHand/Pivot/Pistol/PistolBulletMarker
@onready var attacking: bool = false
@export var camera: Camera2D
Load Scenes
@onready var muzzle_load: PackedScene = preload("res://Scenes/Particles/muzzle.tscn")
@onready var bullet_load: PackedScene = preload("res://Scenes/Props/bullet.tscn")
@onready var death_particle_load: PackedScene = preload("res://Scenes/Particles/player_death_particle.tscn")
var input_vector = Input.get_axis("move_left","move_right")
if input_vector != 0:
apply_acceleration(input_vector, delta)
else:
apply_friction(delta)
if Input.is_action_just_pressed("SwapPlane"):
_on_swapPlane()
if Input.is_action_just_pressed("jump") and is_on_floor():
jump()
if Input.is_action_just_pressed("shoot"):
animator.play("Attacking")
if bullets_amount:
guns_animator.play("Shoot")
move_and_slide()
animate(input_vector)
func process_actions():
if Input.is_action_just_pressed("jump") and is_on_floor():
perform_jump()
if Input.is_action_just_pressed("shoot"):
perform_attack()
func animate(input_vector):
var mouse_position : Vector2 = (get_global_mouse_position() - global_position).normalized()
if mouse_position.x > 0 and animator.flip_h:
animator.flip_h = false
elif mouse_position.x < 0 and not animator.flip_h:
animator.flip_h = true
code
hand.rotation = mouse_position.angle()
if hand.scale.y == 1 and mouse_position.x < 0:
hand.scale.y = -1
elif hand.scale.y == -1 and mouse_position.x > 0:
hand.scale.y = 1
if state != State.ATTACK:
if is_on_floor():
if input_vector != 0:
animator.play("Run")
else:
animator.play("Idle")
else:
if velocity.y > 0:
animator.play("Jump")
# Check if the current animation is the attack animation
$Hand/AttackArea/CollisionShape2D.disabled = true;
state = State.IDLE
func update_animation_state():
if state == State.ATTACK:
return
code
if is_on_floor():
if velocity.x != 0:
animator.play("Run")
else:
animator.play("Idle")
else:
animator.play("Jump") if velocity.y < 0 else animator.play("Fall")
func _on_hurtbox_area_entered(_area):
if not _area.is_in_group("Bullet"):
hit_animator.play("Hit")
take_damage(1) # If damage is to be variable, extract value from _area
EventManager.frame_freeze.emit()
func spawn_death_particle():
var death_particle = death_particle_load.instantiate()
death_particle.global_position = global_position
get_tree().current_scene.add_child(death_particle)
Override PlayerBase method
func take_damage(damage: int) -> void:
player_base.health -= damage # Use the health property from PlayerBase
if player_base.health <= 0:
die()
else:
AudioManager.play_sound(AudioManager.HURT)
func _on_swapPlane():
PlayerState.set_last_2d_position(global_position);
SceneChanger.change_to_3d(global_position) # Calls the singleton to change to 3D scene
Implement the move method required by PlayerBase, the actual movement
logic is in the _physics_process function above.
func move(input_dir: Vector2) -> void:
# For Player2D, the move function is handled by process_movement_input,
# so no additional logic is needed here.
pass
Implement the attack method required by PlayerBase. This is a placeholder
and the actual logic for starting an attack is in the perform_attack function.
func attack() -> void:
perform_attack()
code
# Adjust the jump method to use jump_strength from PlayerBase
func _on_health_changed(new_health: int) -> void:
player_base.health = new_health # Make sure to update it when health changes
# Handle health change (e.g., update UI)
Below is the rewritten script for your 3D character with added functionality for switching to a 3D plane. Note that references to UI elements and the use of signals may be specific to your existing code and may need adjustment to fit your project's signal names and node paths.
gdscript
extends CharacterBody3D
const MAX_LIFEPOINTS := 200
export var speed := 5.0
export var jump_velocity := 4.5
var _lifepoints: int = MAX_LIFEPOINTS setget set_lifepoints, get_lifepoints
var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity")
var _attackTimer: Timer
var _visual: Node3D
var _playerLifebar: ProgressBar
var _animationTree: AnimationTree
# References for swapping to 3D
var sceneChangerScript = preload("res://Scripts/Global/SceneChanger.gd")
var sceneChangerNode: Node
func _ready():
_playerLifebar = get_tree().current_scene.get_node("HUD/PlayerLifeBar")
_playerLifebar.max_value = MAX_LIFEPOINTS
_playerLifebar.value = _lifepoints
_attackTimer = get_node("AttackCooldown")
_visual = get_node("Visual")
_animationTree = get_node("AnimationTree")
sceneChangerNode = get_node("SceneChanger")
func set_lifepoints(value: int):
_lifepoints = clamp(value, 0, MAX_LIFEPOINTS)
_playerLifebar.value = _lifepoints
func get_lifepoints() -> int:
return _lifepoints
func _process(delta: float) -> void:
# The life points are handled by the setter function
pass
func _physics_process(delta: float) -> void:
var velocity := get_velocity()
# Add gravity
if not is_on_floor():
velocity.y -= gravity * delta
# Handle movement and deceleration
var input_dir := Vector2(
Input.get_action_strength("move_right") - Input.get_action_strength("move_left"),
Input.get_action_strength("move_back") - Input.get_action_strength("move_forward")
)
var direction := (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, speed * delta)
velocity.z = lerp(velocity.z, 0, speed * delta)
# Movement and animation logic
# ... (rest of your movement and animation logic here)
# Check for switching to 3D plane
if Input.is_action_just_pressed("SwapPlane"):
_on_SwapPlane()
func _on_SwapPlane():
# Pass additional arguments if necessary, for example passing the player's position
sceneChangerScript.change_to_3d() # Modify the method call according to your singleton's function
And ensure that you have a script, SceneChanger.gd, that has a method change_to_3d(), which performs the scene swapping logic.
In the 2D script, you have a function _on_swapPlane() which calls SceneChanger.change_to_3d(global_position). The 3D script should have a similar method to switch back to 2D or to another 3D scene. You will need to replace change_to_3d() with the actual method name that performs the swapping in your SceneChanger script.
Please make sure that you have set up a signal connection or autoload singleton for scene changes to correctly transition between 2D and 3D planes in your game.