week 5 enemies pickups playerdeath

This commit is contained in:
OddlyTimbot committed 2024-08-12 21:10:22 -04:00
1 parent 04d49fee67
commit e4be043988
48 files changed
+1197 -157

No files matched your search

+82 -76
View File
@@ -17,88 +17,94 @@ var faceLeft = false
var pushLeftEnabled = false
var pushRightEnabled = false
var pushTarget
var living = true
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
# 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)
if not faceLeft:
# facing right
myBullet.transform = marker_right.global_transform
else:
# facing left
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():
print("kill the player")
animated_sprite.play("death")
living = false
# 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():
print("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)
if not faceLeft:
# facing right
myBullet.transform = marker_right.global_transform
else:
# facing left
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():
print("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():
print("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
if left_ray.is_colliding():
print("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)
+40
View File
@@ -0,0 +1,40 @@
extends Area2D
const speed = 60
var direction = 1
@onready var right_cast = $RightCast
@onready var left_cast = $LeftCast
@onready var right_floor_cast = $rightFloorCast
@onready var left_floor_cast = $leftFloorCast
@onready var sprite = $AnimatedSprite2D
@onready var game = %GameController
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
if right_cast.is_colliding():
if not right_cast.get_collider().is_in_group("player"):
direction = -1
sprite.flip_h = true
if left_cast.is_colliding():
if not left_cast.get_collider().is_in_group("player"):
direction = 1
sprite.flip_h = false
if not right_floor_cast.is_colliding():
direction = -1
sprite.flip_h = true
if not left_floor_cast.is_colliding():
direction = 1
sprite.flip_h = false
#move the dude
position.x += direction * speed * delta
func _on_body_entered(body):
if body.is_in_group("player"):
game.playerDeath()
+19
View File
@@ -0,0 +1,19 @@
extends Area2D
@onready var game = %GameController
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
func _on_body_entered(body):
if body.is_in_group("player"):
print("something hit the coin")
game.coinCollected()
queue_free()
+19 -3
View File
@@ -1,7 +1,12 @@
extends Node2D
extends Node
var myBox = preload("res://scenes/crate.tscn")
@onready var box_trap = $BoxTrap
@onready var box_trap = $"../BoxTrap"
@onready var timer = $Timer
@onready var player = $"../CharacterBody2D"
var totalCoins = 0
# Called when the node enters the scene tree for the first time.
func _ready():
@@ -22,7 +27,18 @@ func _on_area_2d_areatrigger(effect, body):
if body.is_in_group("player"):
for n in 3:
var box = myBox.instantiate()
add_child(box)
owner.add_child(box)
box.position = box_trap.position
"powerup":
print("you got power")
func coinCollected():
totalCoins += 1
print("Game controller will add coin " + str(totalCoins) )
func playerDeath():
player.die()
timer.start()
func resetWorld():
get_tree().reload_current_scene()