week 6 files scene and game manager
This commit is contained in:
1 parent
a7be9688f5
commit
9356bd1c7c
296 files changed
+4194
-11
No files matched your search
@@ -0,0 +1,134 @@
|
||||
extends CharacterBody2D
|
||||
|
||||
@export var SPEED = 300.0
|
||||
@export var JUMP_VELOCITY = -300.0
|
||||
@export var BUMP_FORCE = 80.0
|
||||
@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 = $AnimatedSprite2D
|
||||
|
||||
signal playerDead
|
||||
|
||||
var faceLeft = false
|
||||
var pushLeftEnabled = false
|
||||
var pushRightEnabled = false
|
||||
var pushTarget
|
||||
|
||||
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")
|
||||
|
||||
var living = true
|
||||
var animPlaying:String = "idle"
|
||||
|
||||
func die():
|
||||
#kill the player
|
||||
print("kill the player")
|
||||
living = false
|
||||
animPlaying = "death"
|
||||
animated_sprite.play(animPlaying)
|
||||
func hurt():
|
||||
animPlaying = "hurt"
|
||||
animated_sprite.play(animPlaying)
|
||||
|
||||
func _physics_process(delta):
|
||||
if living and animPlaying !="hurt":
|
||||
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)
|
||||
#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")
|
||||
|
||||
# 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:
|
||||
animPlaying = "idle"
|
||||
|
||||
else:
|
||||
animPlaying = "run"
|
||||
|
||||
else:
|
||||
animPlaying = "jump"
|
||||
|
||||
|
||||
animated_sprite.play(animPlaying)
|
||||
|
||||
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)
|
||||
|
||||
|
||||
func _on_animated_sprite_2d_animation_finished():
|
||||
print("animation finished")
|
||||
if animPlaying == "death":
|
||||
print("reset the game")
|
||||
playerDead.emit()
|
||||
if animPlaying == "hurt":
|
||||
print("hurt anim complete")
|
||||
animPlaying = "idle"
|
||||
|
||||
|
||||
|
||||
func _on_animated_sprite_2d_animation_looped():
|
||||
pass
|
||||
@@ -0,0 +1,8 @@
|
||||
extends Area2D
|
||||
|
||||
@export var effect = "alert"
|
||||
signal areatrigger(effect, Object)
|
||||
|
||||
func _on_body_entered(body):
|
||||
print("detected a body")
|
||||
areatrigger.emit(effect, body)
|
||||
@@ -0,0 +1,36 @@
|
||||
extends Area2D
|
||||
|
||||
const speed = 60
|
||||
var direction = 1
|
||||
@onready var cast_right = $CastRight
|
||||
@onready var cast_left = $CastLeft
|
||||
@onready var sprite = $AnimatedSprite2D
|
||||
@onready var gameController = %GameManager
|
||||
@onready var cast_floor_right = $CastFloorRight
|
||||
@onready var cast_floor_left = $CastFloorLeft
|
||||
|
||||
signal playerDamage
|
||||
|
||||
# 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 not cast_floor_left.is_colliding():
|
||||
direction = 1
|
||||
sprite.flip_h = false
|
||||
position.x += direction * speed * delta
|
||||
|
||||
|
||||
func _on_body_entered(body):
|
||||
if body.is_in_group("player"):
|
||||
GameManager.playerDamage()
|
||||
playerDamage.emit()
|
||||
@@ -0,0 +1,14 @@
|
||||
extends Area2D
|
||||
|
||||
var speed = 750
|
||||
|
||||
func setSpeed(speedVal):
|
||||
speed = speedVal
|
||||
|
||||
func _physics_process(delta):
|
||||
position += transform.x * speed * delta
|
||||
|
||||
func _on_body_entered(body):
|
||||
print("bullet hit something")
|
||||
if not body.is_in_group("player"):
|
||||
queue_free()
|
||||
@@ -0,0 +1,10 @@
|
||||
extends Area2D
|
||||
|
||||
@onready var game = %SceneManager
|
||||
signal coinCollected
|
||||
|
||||
|
||||
func _on_body_entered(body):
|
||||
if body.is_in_group("player"):
|
||||
coinCollected.emit()
|
||||
queue_free()
|
||||
@@ -0,0 +1,16 @@
|
||||
extends Node
|
||||
|
||||
var player: Resource
|
||||
var coinsCollected:int = 0
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
print("game manager ready")
|
||||
player = load("res://scripts/resources/player_stats.tres")
|
||||
|
||||
func resetPlayer():
|
||||
player.health = player.max_health
|
||||
func playerDamage():
|
||||
#replace this damage amount with value from data object
|
||||
player.health -=20
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
extends Node
|
||||
|
||||
#This node is a scene manager. It is used to control all the visible
|
||||
#assets in the viewport. However the scene manager does not encapsulate
|
||||
#overall logic, and it does not persist data - that belongs to the GameManager,
|
||||
#which is an autoload singleton.
|
||||
|
||||
var coinsCollected = 0
|
||||
|
||||
var crate = preload("res://scenes/crate.tscn")
|
||||
@onready var boxtrap = $"../crates/boxtrap"
|
||||
@onready var timer = $Timer
|
||||
@onready var player = $"../CharacterBody2D"
|
||||
|
||||
func _ready():
|
||||
GameManager.resetPlayer()
|
||||
|
||||
func _on_area_2d_areatrigger(effect, body):
|
||||
match effect:
|
||||
"alert":
|
||||
#do stuff
|
||||
if body.is_in_group("player"):
|
||||
for n in 3:
|
||||
var box = crate.instantiate()
|
||||
owner.add_child(box)
|
||||
box.position = boxtrap.position
|
||||
|
||||
func coinCollected():
|
||||
print("scene manager saw coin collected")
|
||||
GameManager.coinsCollected +=1
|
||||
|
||||
func resetGame():
|
||||
timer.start(.5)
|
||||
|
||||
func _on_timer_timeout():
|
||||
get_tree().reload_current_scene()
|
||||
|
||||
#Scene control loop
|
||||
func _process(delta):
|
||||
if GameManager.player.health < 0 && player.living:
|
||||
player.die()
|
||||
|
||||
func _on_badguy_player_damage():
|
||||
player.hurt()
|
||||
@@ -0,0 +1,7 @@
|
||||
class_name CharacterStats
|
||||
extends Resource
|
||||
|
||||
@export var max_health: int = 100
|
||||
@export var starting_health: int = 100
|
||||
@export var health: int = 100
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
[gd_resource type="Resource" script_class="CharacterStats" load_steps=2 format=3 uid="uid://bq0eirh7p7h0u"]
|
||||
|
||||
[ext_resource type="Script" path="res://scripts/resources/character_stats.gd" id="1_4c3kd"]
|
||||
|
||||
[resource]
|
||||
script = ExtResource("1_4c3kd")
|
||||
max_health = 100
|
||||
starting_health = 100
|
||||
health = 100
|
||||
@@ -0,0 +1,9 @@
|
||||
[gd_resource type="Resource" script_class="CharacterStats" load_steps=2 format=3 uid="uid://bi451udylw2l3"]
|
||||
|
||||
[ext_resource type="Script" path="res://scripts/resources/character_stats.gd" id="1_7clgg"]
|
||||
|
||||
[resource]
|
||||
script = ExtResource("1_7clgg")
|
||||
max_health = 50
|
||||
starting_health = 50
|
||||
health = 50
|
||||
Reference in new issue
Block a user