Skip to content
TheChatArchiveThe Chat Archive
gpt-4-1106-preview· openaiHugging Face Suzhen/CodeChat

apply gravity to the entity if it's in 2d plane: extends CharacterBody2D classname…

  1. User

    apply gravity to the entity if it's in 2d plane:

    extends CharacterBody2D class_name Entity

    #var death_scene = var death_config = {} var direction : Vector2 = Vector2()

    var max_health = 10 var current_health : int = 10 var health_regen : int = 1 var armor : int = 0

    var max_mana : int = 100 var current_mana : int = 100 var mana_regen : int = 1

    var max_speed : float = 100 var current_speed : float = 100 var acceleration : float = 4

    var agility : int = 1

    var global_cooldown = 30 var is_busy : bool = false var last_ability : int = 0

    var is_player : bool = false var is_aliv : bool = true

    func get_State(): return { "direction": direction, "max_health": max_health, "current_health": current_health, "health_regen": health_regen, "armor": armor, "max_mana": max_mana, "current_mana": current_mana, "mana_regen": mana_regen, "max_speed": max_speed, "current_speed": current_speed, "acceleration": acceleration, "agility": agility, "global_cooldown": global_cooldown, "is_busy": is_busy, "last_ability": last_ability, "is_player": is_player } func _ready(): add_to_group("entity")

    func get_enemies(): var enemy_group = get_enemy_group() var enemies = get_tree().get_nodes_in_group(enemy_group) return enemies

    func get_enemy_group(): var enemy_group = "friend" if is_player: enemy_group = "foe" return enemy_group

    func find_nearest_enemy(): return get_enemies()[0]

    func get_aim_position(): if is_player: return get_global_mouse_position() else: return find_nearest_enemy().global_position

    func regen_health(): if (current_health < max_health): if((health_regen + current_health) > max_health): current_health = max_health else: current_health += health_regen

    func regen_mana(): if (current_mana < max_health): if((mana_regen + current_mana) > max_mana): current_mana = max_mana else: current_mana += mana_regen

    func modify_mana(amount): var new_mana = current_mana + amount if(new_mana < 0): current_mana = 0 if(new_mana > max_mana): current_mana = max_mana else: current_mana += amount

    func can_cast_ability(mana_cost): if(current_mana - mana_cost) >= 0: modify_mana(-mana_cost) return true else: print("not enough mana!") return false

    func apply_damage(amount): if(armor > 0): amount = amount *((100-armor)*0.01) if(current_health > amount): current_health -= amount else: current_health = 0 if(!is_player): #var loot = death_scene.instantiate() #loot.position = self.global_position #loot.config = death_config #get_nod("/root").add_child(loot) self.queue_free() else: die()

    func _physics_process(delta): last_ability += 1 if ((GameManager.count % 60) == 0): regen_health() regen_mana()

    func load_ability(ability_name): var scene = load("res://Scenes/abilities/" + ability_name + "/" + ability_name + ".tscn") var sceneNode = scene.instantiate() add_child(sceneNode) return sceneNode

    func die(): AudioManager.play_sound(AudioManager.DEATH) EventManager.player_died.emit() queue_free()

  2. Assistant

    In the Godot Engine, applying gravity to an entity on a 2D plane usually involves altering the velocity of the object in relation to the gravity force within the _physics_process function. Assuming you want to add gravity to this Entity class that already extends CharacterBody2D, you should introduce a new variable to represent gravity and a vector to hold the velocity that changes due to gravity. You will also need to apply this velocity during every physics frame update.

    Here is an updated version of your Entity class with gravity applied:

    extends CharacterBody2D
    class_name Entity
    
    # Existing code...
    var gravity : float = 98 # Gravity can be tuned to your game design
    var velocity : Vector2 = Vector2()
    
    # The rest of your existing properties...
    
    func _physics_process(delta):
        # Your existing code...
        
        # Apply gravity to velocity
        velocity.y += gravity * delta
        
        # Move the character
        move_and_slide(velocity, Vector2.UP)
    
        # Call the rest of your _physics_process code...
    

    In the example above, I have added a gravity variable set to an arbitrary value of 98, which you can adjust according to the level of gravitational pull you desire. The velocity variable will update the y-component based on the gravity.

    The move_and_slide function is then used to move the CharacterBody2D along with the applied velocity, using Vector2.UP (which is (0, -1)) as the up_direction parameter, indicating that it considers up to be in the negative Y direction, which is the convention for most 2D coordinate systems.

    Keep in mind that Godot already provides apply_central_impulse, move_and_collide, and other movement-related functions, which we wrap in move_and_slide to handle the gravity movement more gracefully in this case, considering possible collisions with other objects in the game world. If your character interacts differently with its environment, you may need to adjust the code accordingly.