graphics for bullet, crate, animation for player, state machine
This commit is contained in:
1 parent
8a78da855c
commit
a7e18c2689
21 files changed
+569
-35
No files matched your search
@@ -0,0 +1,13 @@
|
||||
class_name Bullet extends Area2D
|
||||
var speed = 700
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
pass # Replace with function body.func setSpeed(value):
|
||||
|
||||
func setSpeed(value):
|
||||
speed = value
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _physics_process(delta: float) -> void:
|
||||
position += transform.x * speed * delta
|
||||
@@ -0,0 +1 @@
|
||||
uid://q1ok5ukbtibm
|
||||
@@ -8,22 +8,83 @@ const JUMP_VELOCITY = -400.0
|
||||
@onready var left_cast: RayCast2D = $LeftCast
|
||||
@onready var right_spawn: Node2D = $RightSpawn
|
||||
@onready var left_spawn: Node2D = $LeftSpawn
|
||||
@onready var player_graphic: AnimatedSprite2D = $PlayerGraphic
|
||||
|
||||
|
||||
enum FaceDirection{LEFT, RIGHT}
|
||||
var facing:FaceDirection = FaceDirection.RIGHT
|
||||
|
||||
enum State{IDLE, RUN, JUMP, FALLING}
|
||||
var current_state:State = State.IDLE
|
||||
|
||||
var pushTarget
|
||||
var pushEnabled = false
|
||||
var direction
|
||||
var upJump = false
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
#game loop
|
||||
handle_input()
|
||||
#calculate the movement
|
||||
handle_movement(delta)
|
||||
#change states
|
||||
update_states()
|
||||
#play animations
|
||||
update_animation()
|
||||
#collision with objects, raycasts
|
||||
|
||||
|
||||
move_and_slide()
|
||||
handle_collisions()
|
||||
|
||||
func update_states():
|
||||
match current_state:
|
||||
#idle when movement in x
|
||||
State.IDLE when velocity.x !=0:
|
||||
current_state = State.RUN
|
||||
State.RUN:
|
||||
if velocity.x ==0:
|
||||
current_state = State.IDLE
|
||||
#jumping when reaching apex
|
||||
State.JUMP when velocity.y > 0:
|
||||
current_state = State.FALLING
|
||||
State.FALLING when is_on_floor():
|
||||
if velocity.x == 0:
|
||||
current_state = State.IDLE
|
||||
else:
|
||||
current_state = State.RUN
|
||||
|
||||
func update_animation():
|
||||
match current_state:
|
||||
State.IDLE:
|
||||
player_graphic.play("idle")
|
||||
State.RUN:
|
||||
player_graphic.play("run")
|
||||
State.JUMP:
|
||||
if upJump:
|
||||
player_graphic.play("jump")
|
||||
State.FALLING:
|
||||
player_graphic.play("falling")
|
||||
func handle_movement(delta):
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
|
||||
# Handle jump.
|
||||
|
||||
if direction:
|
||||
velocity.x = direction * SPEED
|
||||
if direction <0:
|
||||
facing = FaceDirection.LEFT
|
||||
player_graphic.flip_h = true
|
||||
if direction >0:
|
||||
facing = FaceDirection.RIGHT
|
||||
player_graphic.flip_h = false
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
func handle_input():
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
current_state = State.JUMP
|
||||
upJump = true
|
||||
|
||||
if Input.is_action_just_pressed("shove") && pushEnabled:
|
||||
print("shove pressed")
|
||||
@@ -43,17 +104,8 @@ func _physics_process(delta: float) -> void:
|
||||
|
||||
# Get the input direction and handle the movement/deceleration.
|
||||
# As good practice, you should replace UI actions with custom gameplay actions.
|
||||
var direction := Input.get_axis("ui_left", "ui_right")
|
||||
if direction:
|
||||
velocity.x = direction * SPEED
|
||||
if direction <0:
|
||||
facing = FaceDirection.LEFT
|
||||
if direction >0:
|
||||
facing = FaceDirection.RIGHT
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
move_and_slide()
|
||||
direction = Input.get_axis("ui_left", "ui_right")
|
||||
func handle_collisions():
|
||||
for i in get_slide_collision_count():
|
||||
var c = get_slide_collision(i)
|
||||
if c.get_collider() is RigidBody2D:
|
||||
@@ -76,5 +128,9 @@ func _physics_process(delta: float) -> void:
|
||||
pushEnabled = true
|
||||
if not left_cast.is_colliding() && not right_cast.is_colliding():
|
||||
pushEnabled = false
|
||||
|
||||
|
||||
|
||||
|
||||
func _on_animation_finished() -> void:
|
||||
match current_state:
|
||||
State.JUMP:
|
||||
upJump = false
|
||||
@@ -3,6 +3,12 @@ extends Node2D
|
||||
var bullet = preload("res://Scenes/bullet.tscn")
|
||||
var bulletArray =[]
|
||||
var totalAllowedBullets = 7
|
||||
@onready var triggers: Node2D = $"../Triggers"
|
||||
@onready var gameController: Node2D = $".."
|
||||
|
||||
func _ready() -> void:
|
||||
for obj in triggers.get_children():
|
||||
obj.areatrigger.connect(gameController._on_areatrigger)
|
||||
|
||||
#makes bullets
|
||||
func bulletFactory():
|
||||
|
||||
Reference in new issue
Block a user