Learning GDScript
#5
Features so far:
Player ship can fly towards the mouse.
You can pick up an item which levels up your max speed, acceleration and rotation speed.

Todays challenge: Make a boost function and make upgrades for it.
New GlobalPlayer additions:
Code:
extends Node

# defines all player stats

var hp_lvl = 1
var shield_lvl = 1
var shot_lvl = 1
var speed_lvl = 1
var accel_lvl = 1
var rot_lvl = 1
var boost_lvl = 1

var money = 0


var stats = {
    "hull": [0, 6, 9, 12, 16],
    "shield": [0, 5, 7, 9, 12],
    "shot": [0, 6, 9, 12, 16],
    "speed": [0, 100, 140, 180, 220],
    "accel": [0, 3, 4, 6, 8],
    "rotation": [0, 0.02, 0.027, 0.034, 0.041],
    "boost": [0, 3.3, 1.4, 1.5, 1.6],
}
(Hint: later there will be a currency system, of course).

Before today, the game checked for input from the 'W' key, (if Input.is_action_pressed("ui_up"): ) and when it was pressed, you accelerated to your max speed (at level 1, that's 100 pixels per second, level 2 is 140).

Now it does that but also checks for the shift key "ui_boost". When pressed, it multiplies your current boost level's dictionary data by your current speed level's dictionary data. All the variables are set before this part.
Code:
if Input.is_action_pressed("ui_boost"):
            max_speed = GlobalPlayer.stats.speed[cur_speed_lvl] * boost

            velocity.x = clamp(velocity.x, -max_speed, max_speed)
            velocity.y = clamp(velocity.y, -max_speed, max_speed)

            velocity += dir * accel * boost
            $Camera2D.zoom.x = lerp($Camera2D.zoom.x, 1, 0.03)
            $Camera2D.zoom.y = lerp($Camera2D.zoom.y, 1, 0.03)

Thanks to the lerp (linear interpolation), the camera also zooms out smoothly while boosting. When you release the key, it zooms back in smoothly. Really helps sell the feeling of speed.

Did I say that I also made the ship have particle effects? Well it swaps between two versions, normal or one with more particles when boosting.

Code:
    if Input.is_action_just_pressed("ui_boost"):
        $Sprite/ShipTrail.emitting = false
        $Sprite/ShipTrailBoost.emitting = true


    if Input.is_action_just_released("ui_boost"):
        $Sprite/ShipTrail.emitting = true
        $Sprite/ShipTrailBoost.emitting = false

    # reset camera
    $Camera2D.zoom.x = lerp($Camera2D.zoom.x, 0.8, 0.01)
    $Camera2D.zoom.y = lerp($Camera2D.zoom.y, 0.8, 0.01)

So now I have a functional boost system that I can upgrade!


Attached Files
.png   Boost.png (Size: 556.45 KB / Downloads: 1)
Reply }


Messages In This Thread
Learning GDScript - by MetalRenard - 02-09-2021, 08:55 AM
RE: Learning GDScript - by MetalRenard - 02-09-2021, 10:45 AM
RE: Learning GDScript - by kyonides - 02-10-2021, 05:55 AM
RE: Learning GDScript - by MetalRenard - 02-10-2021, 04:16 PM
RE: Learning GDScript - by MetalRenard - 02-14-2021, 04:24 PM
RE: Learning GDScript - by MetalRenard - 02-19-2021, 08:25 AM
RE: Learning GDScript - by MetalRenard - 02-23-2021, 10:40 AM
RE: Learning GDScript - by MetalRenard - 03-03-2021, 12:54 PM



Users browsing this thread: 1 Guest(s)