player death working
This commit is contained in:
1 parent
eebf4c2922
commit
03f366b0e1
43 files changed
+287
-138
No files matched your search
@@ -23,79 +23,86 @@ var bullet = preload("res://scenes/bullet.tscn")
|
||||
# Get the gravity from the project settings to be synced with RigidBody nodes.
|
||||
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
|
||||
|
||||
func _physics_process(delta):
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity.y += gravity * delta
|
||||
var living = true
|
||||
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
# Hand force push (melee attack)
|
||||
if Input.is_action_just_pressed("push") && pushRightEnabled && faceLeft == false:
|
||||
pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 5)
|
||||
pushRightEnabled = false
|
||||
if Input.is_action_just_pressed("push") && pushLeftEnabled:
|
||||
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 5)
|
||||
pushLeftEnabled = false
|
||||
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
#make a bullet
|
||||
var myBullet = bullet.instantiate()
|
||||
#set bullet speed?
|
||||
|
||||
#put the bullet at the Marker
|
||||
owner.add_child(myBullet)
|
||||
#facing left or right?
|
||||
if not faceLeft:
|
||||
myBullet.transform = marker_right.global_transform
|
||||
else:
|
||||
myBullet.transform = marker_left.global_transform
|
||||
myBullet.setSpeed(-750)
|
||||
|
||||
# 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")
|
||||
func die():
|
||||
#kill the player
|
||||
print("kill the player")
|
||||
living = false
|
||||
animated_sprite.play("death")
|
||||
|
||||
# In this example we have replaced the faceLeft code to also have
|
||||
# the character's sprite flip depending what direction they are facing
|
||||
if direction:
|
||||
faceLeft = true if direction<0 else false
|
||||
animated_sprite.flip_h = true if direction < 0 else false
|
||||
velocity.x = direction * SPEED
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
if is_on_floor():
|
||||
if direction==0:
|
||||
animated_sprite.play("idle")
|
||||
else:
|
||||
animated_sprite.play("run")
|
||||
else:
|
||||
animated_sprite.play("jump")
|
||||
func _physics_process(delta):
|
||||
if living:
|
||||
if not is_on_floor():
|
||||
velocity.y += gravity * delta
|
||||
|
||||
move_and_slide()
|
||||
if right_ray.is_colliding():
|
||||
var collider = right_ray.get_collider()
|
||||
#what did we hit?
|
||||
if collider is Node:
|
||||
if collider.is_in_group("boxes"):
|
||||
print("This is a pushable box")
|
||||
pushRightEnabled = true
|
||||
pushTarget = collider
|
||||
else:
|
||||
pushRightEnabled = false
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
# Hand force push (melee attack)
|
||||
if Input.is_action_just_pressed("push") && pushRightEnabled && faceLeft == false:
|
||||
pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 5)
|
||||
pushRightEnabled = false
|
||||
if Input.is_action_just_pressed("push") && pushLeftEnabled:
|
||||
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 5)
|
||||
pushLeftEnabled = false
|
||||
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
#make a bullet
|
||||
var myBullet = bullet.instantiate()
|
||||
#set bullet speed?
|
||||
|
||||
#put the bullet at the Marker
|
||||
owner.add_child(myBullet)
|
||||
#facing left or right?
|
||||
if not faceLeft:
|
||||
myBullet.transform = marker_right.global_transform
|
||||
else:
|
||||
myBullet.transform = marker_left.global_transform
|
||||
myBullet.setSpeed(-750)
|
||||
|
||||
# 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 left_ray.is_colliding():
|
||||
var collider = left_ray.get_collider()
|
||||
if collider is Node:
|
||||
if collider.is_in_group("boxes"):
|
||||
pushLeftEnabled = true
|
||||
pushTarget = collider
|
||||
else:
|
||||
pushLeftEnabled = false
|
||||
|
||||
#loop through the collisions
|
||||
for i in get_slide_collision_count():
|
||||
var c = get_slide_collision(i)
|
||||
if c.get_collider() is RigidBody2D:
|
||||
c.get_collider().apply_central_impulse(-c.get_normal()* BUMP_FORCE)
|
||||
# In this example we have replaced the faceLeft code to also have
|
||||
# the character's sprite flip depending what direction they are facing
|
||||
if direction:
|
||||
faceLeft = true if direction<0 else false
|
||||
animated_sprite.flip_h = true if direction < 0 else false
|
||||
velocity.x = direction * SPEED
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
if is_on_floor():
|
||||
if direction==0:
|
||||
animated_sprite.play("idle")
|
||||
else:
|
||||
animated_sprite.play("run")
|
||||
else:
|
||||
animated_sprite.play("jump")
|
||||
|
||||
move_and_slide()
|
||||
if right_ray.is_colliding():
|
||||
var collider = right_ray.get_collider()
|
||||
#what did we hit?
|
||||
if collider is Node:
|
||||
if collider.is_in_group("boxes"):
|
||||
pushRightEnabled = true
|
||||
pushTarget = collider
|
||||
else:
|
||||
pushRightEnabled = false
|
||||
|
||||
if left_ray.is_colliding():
|
||||
var collider = left_ray.get_collider()
|
||||
if collider is Node:
|
||||
if collider.is_in_group("boxes"):
|
||||
pushLeftEnabled = true
|
||||
pushTarget = collider
|
||||
else:
|
||||
pushLeftEnabled = false
|
||||
|
||||
#loop through the collisions
|
||||
for i in get_slide_collision_count():
|
||||
var c = get_slide_collision(i)
|
||||
if c.get_collider() is RigidBody2D:
|
||||
c.get_collider().apply_central_impulse(-c.get_normal()* BUMP_FORCE)
|
||||
@@ -6,17 +6,26 @@ var direction = 1
|
||||
@onready var cast_left = $CastLeft
|
||||
@onready var sprite = $AnimatedSprite2D
|
||||
@onready var game = %GameManager
|
||||
@onready var cast_floor_right = $CastFloorRight
|
||||
@onready var cast_floor_left = $CastFloorLeft
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta):
|
||||
if cast_right.is_colliding():
|
||||
if not cast_right.get_collider().is_in_group("player"):
|
||||
direction = -1
|
||||
sprite.flip_h = true
|
||||
if cast_left.is_colliding():
|
||||
if not cast_left.get_collider().is_in_group("player"):
|
||||
direction = 1
|
||||
sprite.flip_h = false
|
||||
if not cast_floor_right.is_colliding():
|
||||
direction = -1
|
||||
sprite.flip_h = true
|
||||
if cast_left.is_colliding():
|
||||
if not cast_floor_left.is_colliding():
|
||||
direction = 1
|
||||
sprite.flip_h = false
|
||||
|
||||
sprite.flip_h = false
|
||||
position.x += direction * speed * delta
|
||||
|
||||
|
||||
|
||||
@@ -6,3 +6,4 @@ extends Area2D
|
||||
func _on_body_entered(body):
|
||||
if body.is_in_group("player"):
|
||||
game.coinCollected()
|
||||
queue_free()
|
||||
@@ -4,7 +4,7 @@ var coinsCollected = 0
|
||||
|
||||
var crate = preload("res://scenes/crate.tscn")
|
||||
@onready var boxtrap = $"../crates/boxtrap"
|
||||
|
||||
@onready var timer = $Timer
|
||||
@onready var player = $"../CharacterBody2D"
|
||||
|
||||
func _on_area_2d_areatrigger(effect, body):
|
||||
@@ -26,5 +26,8 @@ func coinCollected():
|
||||
print("coins collected "+str(coinsCollected) )
|
||||
|
||||
func playerDeath():
|
||||
print("kill the player")
|
||||
|
||||
player.die()
|
||||
timer.start()
|
||||
|
||||
func _on_timer_timeout():
|
||||
get_tree().reload_current_scene()
|
||||
Reference in new issue
Block a user