adding character autopilot
This commit is contained in:
1 parent
fa5025b626
commit
46889c32f3
5 files changed
+69
-35
No files matched your search
@@ -46,13 +46,11 @@ func _process(delta: float) -> void:
|
||||
|
||||
|
||||
func _on_trigger(body: Variant, effect, trigger) -> void:
|
||||
print("GC knows trigger...."+effect)
|
||||
if body is Crate:
|
||||
match effect:
|
||||
"destroy":
|
||||
destroySignal.emit(body)
|
||||
"teleport":
|
||||
print("GC teleport an object")
|
||||
teleportSignal.emit(body)
|
||||
|
||||
if body is Player:
|
||||
@@ -64,7 +62,6 @@ func _on_trigger(body: Variant, effect, trigger) -> void:
|
||||
|
||||
func crateUpdate(cratesAmount)->void:
|
||||
crateTotal = cratesAmount
|
||||
print("GC updated crates: "+str(crateTotal))
|
||||
if crateTotal <=0:
|
||||
print("You WON!!!")
|
||||
currentLevel +=1
|
||||
@@ -84,12 +81,10 @@ func bulletDamage(body:Node2D, bullet:Bullet)->void:
|
||||
destroySignal.emit(bullet)
|
||||
|
||||
func _on_coin_collected(body:Node2D, coin:Coin)->void:
|
||||
print("GC knows coin collected")
|
||||
coinsCollected +=1
|
||||
destroySignal.emit(coin)
|
||||
|
||||
func totalCoins(value)->void:
|
||||
print("GC knows coins remaining "+str(value))
|
||||
if value <=0:
|
||||
#no coins left in level
|
||||
currentLevel +=1
|
||||
@@ -99,8 +94,6 @@ func totalCoins(value)->void:
|
||||
|
||||
func _on_slime_damage(body:Node2D, slime:Slime)->void:
|
||||
playerHealth -= enemiesDict[slime]["damage"]
|
||||
print("GC knows damage from slime "+str(enemiesDict[slime]["damage"]))
|
||||
print("GC knows slime damage to player "+str(playerHealth))
|
||||
if playerHealth <=0:
|
||||
print("YOU DED")
|
||||
levelChangeSignal.emit(levels[currentLevel])
|
||||
|
||||
+58
-20
@@ -3,6 +3,8 @@ class_name Player extends CharacterBody2D
|
||||
@onready var right_target: Marker2D = $RightTarget
|
||||
@onready var left_target: Marker2D = $LeftTarget
|
||||
@onready var player_graphic: AnimatedSprite2D = $PlayerGraphic
|
||||
@onready var right_down_cast: RayCast2D = $RightDownCast
|
||||
@onready var left_down_cast: RayCast2D = $LeftDownCast
|
||||
|
||||
|
||||
const SPEED = 300.0
|
||||
@@ -11,41 +13,66 @@ const BUMP_POWER = 50
|
||||
enum FaceDirection{LEFT, RIGHT}
|
||||
var facing:FaceDirection = FaceDirection.RIGHT
|
||||
var direction
|
||||
#awarness
|
||||
var cliffEdge = false
|
||||
enum PlayerState{IDLE,RUNNING, JUMPING, FALLING}
|
||||
var current_player_state:PlayerState = PlayerState.IDLE
|
||||
var jumpUp:bool = false
|
||||
|
||||
func _ready() -> void:
|
||||
var timer = Timer.new()
|
||||
add_child(timer)
|
||||
|
||||
timer.wait_time = 3.0 # Wait time in seconds
|
||||
timer.one_shot = false # Set to false for repeating
|
||||
timer.autostart = true # Start automatically on enter
|
||||
timer.timeout.connect(handle_ai)
|
||||
timer.start()
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
|
||||
handle_input()
|
||||
#handle_input()
|
||||
handle_movement(delta)
|
||||
handle_state()
|
||||
handle_animation()
|
||||
move_and_slide()
|
||||
handle_collisions()
|
||||
|
||||
func handle_input()->void:
|
||||
direction = Input.get_axis("ui_left", "ui_right")
|
||||
if direction <0:
|
||||
player_graphic.flip_h = true
|
||||
if direction >0:
|
||||
player_graphic.flip_h = false
|
||||
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
|
||||
func handle_ai()->void:
|
||||
var ran = randi()%10
|
||||
#running, idle
|
||||
print("random number "+str(ran))
|
||||
direction = 0
|
||||
if ran<=4:
|
||||
direction = -1
|
||||
if ran>=7:
|
||||
direction = 1
|
||||
|
||||
#shooting
|
||||
var shootran = randi()%5
|
||||
if shootran >3:
|
||||
shoot()
|
||||
|
||||
func jump()->void:
|
||||
if is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
current_player_state = PlayerState.JUMPING
|
||||
jumpUp = true
|
||||
# Handle Shoot
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
print("Player wants to shoot")
|
||||
match facing:
|
||||
func shoot()->void:
|
||||
match facing:
|
||||
FaceDirection.RIGHT:
|
||||
print("Shoot to the right")
|
||||
%SceneManager.makeBullet(right_target.global_transform, 700)
|
||||
FaceDirection.LEFT:
|
||||
print("Shoot to the left")
|
||||
%SceneManager.makeBullet(left_target.global_transform, -700)
|
||||
|
||||
func handle_input()->void:
|
||||
direction = Input.get_axis("ui_left", "ui_right")
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_accept"):
|
||||
jump()
|
||||
# Handle Shoot
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
shoot()
|
||||
func handle_state()->void:
|
||||
match current_player_state:
|
||||
PlayerState.IDLE when velocity.x !=0:
|
||||
@@ -73,6 +100,7 @@ func handle_animation()->void:
|
||||
player_graphic.play("falling")
|
||||
|
||||
func handle_movement(delta)->void:
|
||||
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
@@ -80,10 +108,13 @@ func handle_movement(delta)->void:
|
||||
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_collisions()->void:
|
||||
for i in get_slide_collision_count():
|
||||
var c = get_slide_collision(i)
|
||||
@@ -91,9 +122,16 @@ func handle_collisions()->void:
|
||||
c.get_collider().apply_central_impulse(-c.get_normal()*BUMP_POWER)
|
||||
if c.get_collider() is NPCCharacter:
|
||||
var myCharacter:NPCCharacter = c.get_collider()
|
||||
var direction = -c.get_normal()*BUMP_POWER
|
||||
myCharacter.knockBack(direction)
|
||||
|
||||
var mydirection = -c.get_normal()*BUMP_POWER
|
||||
myCharacter.knockBack(mydirection)
|
||||
if not right_down_cast.is_colliding() and is_on_floor():
|
||||
#on edge of drop on right side
|
||||
if velocity.x > 100:
|
||||
jump()
|
||||
if not left_down_cast.is_colliding() and is_on_floor():
|
||||
#on edge of drop on left side
|
||||
if velocity.x <-100:
|
||||
jump()
|
||||
func _on_animation_finished() -> void:
|
||||
match current_player_state:
|
||||
PlayerState.JUMPING:
|
||||
|
||||
@@ -23,7 +23,6 @@ func _process(delta: float) -> void:
|
||||
pass
|
||||
|
||||
func buildLevel()->void:
|
||||
print("building level")
|
||||
updateCrates()
|
||||
updateTriggers()
|
||||
updateCoins()
|
||||
@@ -111,8 +110,6 @@ func bulletFactory()->Bullet :
|
||||
bulletArray.push_front(myBullet)
|
||||
return myBullet
|
||||
func makeBullet(targetPosition, speed)->void:
|
||||
print("Make a bullet!")
|
||||
print("bullets created: "+str( bulletArray.size() ))
|
||||
var myBullet:Bullet = bulletFactory()
|
||||
myBullet.transform = targetPosition
|
||||
myBullet.setSpeed(speed)
|
||||
|
||||
Reference in new issue
Block a user