0
\$\begingroup\$

Here is my code

extends KinematicBody2D
export var speed = 100
export var walk_speed = 50
export var dash_speed = 500
export var tileSize = 32.0
export var max_speed = 500
onready var current_speed = speed
onready var lazer_beam = preload("res://scenes/LazerBeam.tscn")
onready var bullet = preload("res://scenes/Bullet.tscn")
var lazer = null

const stamina_loss_captain = 1

export var stamina_lost = {
        "Teleport": 180,
        "Attack": 70

    }







export var stam =  {
        "Initial Max": 2000,
        "Current Max": 2000,
        "Regeneration": 100,

        "Current" : 2000

}


class energy :
    var initial_max = 0
    var current_max = 0
    var regeneration = 0
    var current = 0

    func _init(im = 200, cm = 200, regen = 40, cur = 0):
        initial_max = im
        current_max = cm
        regeneration = regen
        current = cur

    func regenerate(delta):
        current += regeneration * delta
        if(current >= current_max):
            current = current_max

    func left():
        if current > 0:
            return true
        else:
            return false
        return false
    func deplete(amount):
        current -= amount

    #If current does not go under zero after depletion return true else return false
    func test_deplete(amount):
        if(current - amount >= 0):
            return true
        else:
            return false

    func if_left_deplete(amount):
        if(left()):
            deplete(amount)
            return true
        else:
            return false

    func if_not_under_zero_when_depleted(amount):
        if(current - amount >= 0):
            deplete(amount)
            return true
        else:
            return false







var stamina = energy.new(stam["Initial Max"], stam["Current Max"], stam["Regeneration"], stam["Current"])



onready var sprite = $Sprite
onready var select_box = preload("res://scenes/SelectHitBox.tscn")
var initpos = Vector2()
var dir = Vector2()
var velocity = Vector2()
var v_length = 0
var counter = 0.0
var moving = false
var jumping = false
var my_side = "N/A"

class entity_input :

    var move_up = false
    var move_down = false
    var move_right = false
    var move_left = false
    var move_h = 0.0
    var move_v = 0.0    
    var attack = 0
    var select = 0
    var walk = 0
    var dash = 0
    var teleport = false
    var tele_pos = Vector2()
    var attack_pos = Vector2()


var ei = entity_input.new()
enum direction { UP, DOWN, LEFT, RIGHT }
var facing = direction.DOWN
var shb = null
func _ready():
    add_to_group("Character")
#   set_z_as_relative(false)
    initpos = position
    pass


func select():
    if(ei.select):

        shb = select_box.instance()
        add_child(shb)
        match facing:
            direction.RIGHT:
                shb.position.x += tileSize
            direction.LEFT:
                shb.position.x -= tileSize
            direction.UP:
                shb.position.y -= tileSize
            direction.DOWN:
                shb.position.y += tileSize



    elif(shb != null):
        shb.queue_free()
        shb = null


func _physics_process(delta):
    stamina.regenerate(delta)


    get_input()

    set_dir(delta)
    if(stamina.left()):
        move(delta)

    select()


    if facing == direction.DOWN:
        sprite.frame = 0
    elif facing == direction.UP:
        sprite.frame = 12
    elif facing == direction.LEFT:
        sprite.frame = 4
    elif facing == direction.RIGHT:
        sprite.frame = 8



    $Label.text = "Stamina: " + str(stamina.current)


#


func set_dir(delta): #set moving
    dir = get_dir(delta)

    if dir.x != 0 or dir.y != 0:

        if dir.x > 0:
            facing = direction.RIGHT
        elif dir.x < 0:
            facing = direction.LEFT
        elif dir.y < 0:
            facing = direction.UP
        elif dir.y > 0:
            facing = direction.DOWN

        moving = true
        initpos = position




func get_dir(delta): #user input
    #pressing a vertical input and horizontal input
    #at the same time it will have
    #diagonal movement
    velocity = Vector2()

    var v_input = (Vector2(ei.move_h, ei.move_v))
    var v_angle = v_input.angle()
    var v_length = 1
    #for game_pad
#   v_length = v_input.length()
    if(ei.dash):

        current_speed = dash_speed
    elif(ei.walk):

        current_speed = walk_speed
    else:

        current_speed = speed

    if(ei.move_h != 0 or ei.move_v != 0):
        velocity += Vector2(current_speed * v_length, 0).rotated(v_angle)
        velocity.x = round(velocity.x)
        velocity.y = round(velocity.y)






    return Vector2(velocity)

func move(delta): # move the player linearly

#   var col_info = move_and_collide(velocity * delta)
#   var remaining_vel = Vector2()
    if(ei.teleport and stamina.if_not_under_zero_when_depleted(stamina_lost["Teleport"])):
        var prev_pos = position
        position = ei.tele_pos
        var check_col = move_and_collide(Vector2())
        if(check_col):
            position = prev_pos

    if(ei.attack and stamina.if_not_under_zero_when_depleted(stamina_lost["Attack"])):

#       lazer = lazer_beam.instance()
#       add_child(lazer)
#       lazer.look_at(ei.attack_pos)
        var bul = bullet.instance()
        get_tree().get_root().add_child(bul)
        bul.position = position
        bul.look_at(ei.attack_pos)
        bul.set_linear_velocity(Vector2(1000, 0).rotated(bul.rotation))


        pass



    var remaining_vel = move_and_slide(velocity)
    stamina.deplete((velocity.length()/500) * delta * 80)



    var col_info = Vector2()
    if(get_slide_count() != 0):
        col_info = get_slide_collision(0)

    handle_collision(delta, col_info, remaining_vel, false)




func handle_collision(delta, col_info, rem_vel, slide = false):

    handle_nonslide_collision(delta, col_info, rem_vel)

func handle_nonslide_collision(delta, col_info, rem_vel):
    if(col_info):
        var bodyHit = col_info.collider
        if(col_info.collider.get_class() == "KinematicBody2D"):

            if(bodyHit.is_in_group("KinematicBox")):
                var bodyHitTex = bodyHit.get_node("Sprite").get_texture()
                var bodyHitSprite = bodyHit.get_node("Sprite")
                var Tex = $Sprite.get_texture()
#               var side = Global.at_what_side(bodyHit.position, bodyHitTex.get_width() / bodyHitSprite.hframes, bodyHitTex.get_height() / bodyHitSprite.vframes, position, tileSize, tileSize)
                var side = bodyHit.side_touched
                match side:
                    "Right":
                        bodyHit.move_and_slide(Vector2(col_info.get_remainder().x, 0))
                        my_side = "Right"
                    "Left":
                        my_side = "Left"
                        bodyHit.move_and_slide(Vector2(col_info.get_remainder().x, 0))
                    "Up":
                        my_side = "Up"
                        bodyHit.move_and_slide(Vector2(0, col_info.get_remainder().y))
                    "Down":
                        my_side = "Down"
                        bodyHit.move_and_slide(Vector2(0, col_info.get_remainder().y))
                    _:
                        my_side = "N/A"
            else:
                bodyHit.move_and_slide(col_info.get_remainder())
                pass

            col_info = move_and_collide(col_info.get_remainder())
            if(col_info):
                v_length = velocity.length() - col_info.get_remainder().length()

        elif(col_info.collider.get_class() == "RigidBody2D"):
            if(col_info.collider.is_in_group("phys_box")):

                var side = bodyHit.sideTouched
                match side:
                    "Right":
                        bodyHit.add_force(Vector2(), Vector2(col_info.get_remainder().x, 0))
                        my_side = "Right"
                    "Left":
                        my_side = "Left"
                        bodyHit.add_force(Vector2(), Vector2(col_info.get_remainder().x, 0))
                    "Up":
                        my_side = "Up"
                        bodyHit.add_force(Vector2(), Vector2(0, col_info.get_remainder().y))
                    "Down":
                        my_side = "Down"
                        bodyHit.add_force(Vector2(), Vector2(0, col_info.get_remainder().y))
                    _:
                        my_side = "N/A"


                bodyHit.col = true
                col_info = move_and_collide(col_info.get_remainder())
                if(col_info):
                    v_length = velocity.length() - col_info.get_remainder().length()

    else:
        v_length = velocity.length()

    col_info = null
    pass


func handle_slide_collision(delta, col_info):
    if(col_info):
        var bodyHit = col_info.collider
        if(col_info.collider.get_class() == "KinematicBody2D"):
            if(bodyHit.is_in_group("KinematicBox")):
                var bodyHitTex = bodyHit.get_node("Sprite").get_texture()
                var bodyHitSprite = bodyHit.get_node("Sprite")
                var Tex = $Sprite.get_texture()
                var side = Global.at_what_side(bodyHit.position, bodyHitTex.get_width() / bodyHitSprite.hframes, bodyHitTex.get_height() / bodyHitSprite.vframes, position)

                match side:
                    "Right":
                        bodyHit.move_and_slide(Vector2(velocity.x, 0))
                        side = "Right"
                    "Left":
                        side = "Left"
                        bodyHit.move_and_slide(Vector2(velocity.x, 0))
                    "Up":
                        side = "Up"
                        bodyHit.move_and_slide(Vector2(0, velocity.y))
                    "Down":
                        side = "Down"
                        bodyHit.move_and_slide(Vector2(0, velocity.y))
                    _:
                        pass
            else:
                bodyHit.move_and_slide(velocity)
                pass

#           col_info = move_and_collide(col_info.get_remainder())
#           if(col_info):
#               v_length = velocity.length() - col_info.get_remainder().length()

#       elif(col_info.collider.get_class() == "RigidBody2D"):
#           col_info.collider.add_force(Vector2(), velocity * delta)
#
#           bodyHit.col = true
#           col_info = move_and_collide(col_info.get_remainder())
#           if(col_info):
#               v_length = velocity.length() - col_info.get_remainder().length()
    else:
        v_length = velocity.length()

    col_info = null
    pass

func get_input():
    pass
\$\endgroup\$
  • \$\begingroup\$ Please edit you question to explain what you are doing and how the result is deferent from your expectations. Please also edit your code to include only the relevant sections. \$\endgroup\$ – Kelly Thomas Aug 31 '18 at 23:54
  • \$\begingroup\$ If you're using godot 3.0.x the export for dictionary does not work well atm. \$\endgroup\$ – LookForAngular Nov 11 '18 at 23:34

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.