week 5 updates player death bad guy coin
This commit is contained in:
1 parent
4be87a6b40
commit
a1820f74c6
39 files changed
+1020
-207
No files matched your search
+30
-24
@@ -4,25 +4,38 @@ extends CharacterBody2D
|
||||
@export var SPEED = 300.0
|
||||
@export var JUMP_VELOCITY = -300.0
|
||||
@export var BUMP_FORCE = 80.0
|
||||
@export var PUSH_FORCE = 750
|
||||
@export var PUSH_FORCE = 200
|
||||
|
||||
@onready var right_ray = $RightRay
|
||||
@onready var left_ray = $LeftRay
|
||||
@onready var marker_right = $MarkerRight
|
||||
@onready var marker_left = $MarkerLeft
|
||||
@onready var animated_sprite_2d = $AnimatedSprite2D
|
||||
|
||||
@onready var animated_sprite = $AnimatedSprite2D
|
||||
|
||||
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 living_status():
|
||||
return living
|
||||
|
||||
func die():
|
||||
print("kill the player")
|
||||
animated_sprite.play("death")
|
||||
living = false
|
||||
|
||||
func _physics_process(delta):
|
||||
if not living:
|
||||
return
|
||||
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity.y += gravity * delta
|
||||
@@ -32,54 +45,48 @@ func _physics_process(delta):
|
||||
velocity.y = JUMP_VELOCITY
|
||||
# Hand force push (melee attack)
|
||||
if Input.is_action_just_pressed("push") && pushRightEnabled && faceLeft == false:
|
||||
print("I should force push")
|
||||
pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 10)
|
||||
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 * 10)
|
||||
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() #you can only instantiate a scene
|
||||
var myBullet = bullet.instantiate()
|
||||
#set bullet speed?
|
||||
|
||||
#put the bullet at the Marker
|
||||
owner.add_child(myBullet) #owner because its adding it to the game and not the character.
|
||||
#This is important because if the character jumps you don't want the bullet to jump as well.
|
||||
owner.add_child(myBullet)
|
||||
#facing left or right?
|
||||
if not faceLeft:
|
||||
#facing right
|
||||
myBullet.transform = marker_right.global_transform
|
||||
else:
|
||||
myBullet.transform = marker_left.global_transform
|
||||
myBullet.setSpeed(-750) #Tim changed it to -750. Magic number. Not sure why.
|
||||
myBullet.setSpeed(-750)
|
||||
|
||||
#remove the pass at the end
|
||||
|
||||
# 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 <0:
|
||||
faceLeft = true
|
||||
if direction >0:
|
||||
faceLeft = 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
|
||||
animated_sprite_2d.flip_h = true if direction < 0 else false
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
if is_on_floor():
|
||||
if direction == 0:
|
||||
animated_sprite_2d.play("Idle")
|
||||
if direction==0:
|
||||
animated_sprite.play("idle")
|
||||
else:
|
||||
animated_sprite_2d.play("Run")
|
||||
animated_sprite.play("run")
|
||||
else:
|
||||
animated_sprite_2d.play("Jump")
|
||||
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:
|
||||
@@ -91,7 +98,6 @@ func _physics_process(delta):
|
||||
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"):
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
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 game = %GameController
|
||||
@onready var player = $"../CharacterBody2D"
|
||||
|
||||
|
||||
# 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
|
||||
|
||||
if left_cast.is_colliding():
|
||||
if not left_cast.get_collider().is_in_group("player"):
|
||||
direction = 1
|
||||
|
||||
if not right_floor_cast.is_colliding() or not left_floor_cast.is_colliding():
|
||||
direction *= -1
|
||||
|
||||
position.x += direction * speed * delta
|
||||
|
||||
|
||||
func _on_body_entered(body):
|
||||
if body.is_in_group("player") and player.living_status():
|
||||
game.playerDeath()
|
||||
+1
-1
@@ -10,5 +10,5 @@ func _physics_process(delta):
|
||||
|
||||
func _on_body_entered(body):
|
||||
print("bullet hit something")
|
||||
if not body.is_in_group("player"): #if it hits something other than the player, then disappear
|
||||
if not body.is_in_group("player"):
|
||||
queue_free()
|
||||
@@ -0,0 +1,19 @@
|
||||
extends Area2D
|
||||
@onready var game_controller = %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_controller.coinCollected()
|
||||
queue_free()
|
||||
+25
-17
@@ -1,28 +1,36 @@
|
||||
extends Node2D
|
||||
extends Node
|
||||
|
||||
var myBox = preload("res://scenes/crate.tscn")
|
||||
@onready var box_trap = $BoxTrap
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
pass # Replace with function body.
|
||||
var crate = preload("res://scenes/crate.tscn")
|
||||
@onready var boxtrap = $"../crates/boxtrap"
|
||||
@onready var timer = $Timer
|
||||
@onready var player = $"../CharacterBody2D"
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta):
|
||||
pass
|
||||
|
||||
var totalCoins = 0
|
||||
|
||||
func _on_area_2d_areatrigger(effect, body):
|
||||
print("Game Controller sees the trigger"+effect)
|
||||
print("Game Controller sees the trigger "+effect)
|
||||
match effect:
|
||||
"alert":
|
||||
#do stuff
|
||||
print("hi")
|
||||
if body.is_in_group("player"):
|
||||
for n in 3:
|
||||
var box = myBox.instantiate()
|
||||
add_child(box)
|
||||
box.position = box_trap.position
|
||||
for n in 3:
|
||||
var box = crate.instantiate()
|
||||
owner.add_child(box)
|
||||
box.position = boxtrap.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()
|
||||
|
||||
|
||||
Reference in new issue
Block a user