contra/Scripts/player2.gd

179 lines
5.5 KiB
GDScript3
Raw Permalink Normal View History

2024-11-12 17:51:04 +00:00
extends CharacterBody2D
2024-11-12 18:52:41 +00:00
const SPEED = 110.0
2024-11-12 18:59:00 +00:00
const JUMP_VELOCITY = -310.0
2024-11-12 17:51:04 +00:00
enum player_states {idle, run, jump, crouch, aimUp, water, dead}
var current_state = player_states.idle
var canShot = true
2024-11-12 18:59:00 +00:00
var normalBullet = preload(res://scenes/bullet_normal.tscn)
var bulletP1 = preload(res://scenes/bullet_p1.tscn)
var bulletP2 = preload(res://scenes/bullet_p2.tscn)
2024-11-12 17:51:04 +00:00
var currentBullet = normalBullet
# Get the gravity from the project settings to be synced with RigidBody nodes.
2024-11-12 18:59:00 +00:00
var gravity = ProjectSettings.get_setting(physics/2d/default_gravity)
2024-11-12 17:51:04 +00:00
var direction
var vDirection
var minCamera
var maxCamera
func _ready():
2024-11-12 18:59:00 +00:00
var allPlayers = get_tree().get_nodes_in_group(player)
2024-11-12 17:51:04 +00:00
for p in allPlayers:
add_collision_exception_with(p)
2024-11-12 18:59:00 +00:00
var cam = get_tree().get_nodes_in_group(camera)
minCamera = cam[0].get_node(min)
maxCamera = cam[0].get_node(max)
2024-11-12 17:51:04 +00:00
func _physics_process(delta):
# Add the gravity.
if not is_on_floor() && current_state != player_states.water:
velocity.y += gravity * delta
if(current_state != player_states.dead):
2024-11-12 18:59:00 +00:00
$AnimationPlayer.play(jump)
2024-11-12 17:51:04 +00:00
current_state = player_states.jump
else:
2024-11-12 18:59:00 +00:00
get_tree().get_nodes_in_group(spawnPoint)[0].global_position.x = global_position.x
2024-11-12 17:51:04 +00:00
# Handle Jump.
if(current_state != player_states.dead):
2024-11-12 18:59:00 +00:00
if Input.is_action_just_pressed(H) and (is_on_floor() || current_state == player_states.water):
2024-11-12 17:51:04 +00:00
velocity.y = JUMP_VELOCITY
current_state = player_states.jump
2024-11-12 18:59:00 +00:00
direction = Input.get_axis(A, D)
vDirection = Input.get_axis(W, S)
2024-11-12 17:51:04 +00:00
if(vDirection):
if is_on_floor():
if vDirection > 0:
if(current_state == player_states.idle):
2024-11-12 18:59:00 +00:00
$AnimationPlayer.play(crouch)
2024-11-12 17:51:04 +00:00
current_state = player_states.crouch
else:
if(current_state == player_states.idle):
2024-11-12 18:59:00 +00:00
$AnimationPlayer.play(aim_up)
2024-11-12 17:51:04 +00:00
current_state = player_states.aimUp
elif current_state == player_states.water:
if(vDirection > 0):
2024-11-12 18:59:00 +00:00
$AnimationPlayer.play(swim_hide)
2024-11-12 17:51:04 +00:00
else:
if current_state == player_states.crouch || current_state == player_states.aimUp:
current_state = player_states.idle
if current_state != player_states.crouch && (current_state != player_states.water || !vDirection):
if direction:
velocity.x = direction * SPEED
if is_on_floor():
if(!vDirection):
if(current_state != player_states.water):
2024-11-12 18:59:00 +00:00
$AnimationPlayer.play(run)
2024-11-12 17:51:04 +00:00
current_state = player_states.run
else:
2024-11-12 18:59:00 +00:00
$AnimationPlayer.play(swim)
2024-11-12 17:51:04 +00:00
elif(current_state == player_states.water):
2024-11-12 18:59:00 +00:00
$AnimationPlayer.play(swim)
2024-11-12 17:51:04 +00:00
elif(vDirection > 0):
2024-11-12 18:59:00 +00:00
$AnimationPlayer.play(down_right)
2024-11-12 17:51:04 +00:00
current_state = player_states.run
elif(vDirection < 0):
2024-11-12 18:59:00 +00:00
$AnimationPlayer.play(up_right)
2024-11-12 17:51:04 +00:00
current_state = player_states.run
elif (current_state == player_states.water):
if(!vDirection):
2024-11-12 18:59:00 +00:00
$AnimationPlayer.play(swim)
2024-11-12 17:51:04 +00:00
$spr.flip_h = (direction > 0)
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
if is_on_floor() && current_state != player_states.aimUp:
if(current_state != player_states.water):
2024-11-12 18:59:00 +00:00
$AnimationPlayer.play(idle)
2024-11-12 17:51:04 +00:00
current_state = player_states.idle
else:
2024-11-12 18:59:00 +00:00
$AnimationPlayer.play(swim_idle)
2024-11-12 17:51:04 +00:00
elif(current_state == player_states.water && !vDirection):
2024-11-12 18:59:00 +00:00
$AnimationPlayer.play(swim_idle)
2024-11-12 17:51:04 +00:00
if(global_position.x < minCamera.global_position.x && velocity.x < 0):
velocity.x = 0
elif(global_position.x > maxCamera.global_position.x && velocity.x > 0):
velocity.x = 0
var collision = move_and_slide()
if(current_state != player_states.dead):
if(collision):
for i in get_slide_collision_count():
var collidedObj = get_slide_collision(i)
2024-11-12 18:59:00 +00:00
if(collidedObj.get_collider().is_in_group(water)):
2024-11-12 17:51:04 +00:00
current_state = player_states.water
2024-11-12 18:59:00 +00:00
elif(collidedObj.get_collider().is_in_group(powerup)):
currentBullet = get(bulletP + str(collidedObj.get_collider().powerType))
2024-11-12 17:51:04 +00:00
collidedObj.get_collider().queue_free()
2024-11-12 18:59:00 +00:00
elif(collidedObj.get_collider().is_in_group(enemy) || collidedObj.get_collider().is_in_group(enemy_bullet)):
2024-11-12 17:51:04 +00:00
death()
2024-11-12 18:59:00 +00:00
if(Input.is_action_just_pressed(G) && canShot):
2024-11-12 17:51:04 +00:00
var newBullet = currentBullet.instantiate()
2024-11-12 18:59:00 +00:00
var lvl = get_tree().get_first_node_in_group(level)
2024-11-12 17:51:04 +00:00
lvl.add_child(newBullet)
var faceDirection = getFacingDirection()
for c in newBullet.get_child_count():
newBullet.get_child(c).setVelocity(faceDirection)
2024-11-12 18:59:00 +00:00
newBullet.get_child(c).add_to_group(id_p2)
newBullet.global_position = get_node(SpawnPositions/Aim_ + faceDirection).global_position
2024-11-12 17:51:04 +00:00
canShot = false
$Timer.start()
func getFacingDirection():
2024-11-12 18:59:00 +00:00
var fDirection =
2024-11-12 17:51:04 +00:00
if(!vDirection && !direction):
if($spr.flip_h):
2024-11-12 18:59:00 +00:00
fDirection = Right
2024-11-12 17:51:04 +00:00
else:
2024-11-12 18:59:00 +00:00
fDirection = Left
2024-11-12 17:51:04 +00:00
elif(direction):
if(direction > 0):
2024-11-12 18:59:00 +00:00
fDirection = Right
2024-11-12 17:51:04 +00:00
else:
2024-11-12 18:59:00 +00:00
fDirection = Left
2024-11-12 17:51:04 +00:00
if(vDirection):
if(vDirection < 0):
2024-11-12 18:59:00 +00:00
fDirection += Up
2024-11-12 17:51:04 +00:00
else:
2024-11-12 18:59:00 +00:00
fDirection += Down
2024-11-12 17:51:04 +00:00
elif(vDirection):
if(vDirection < 0):
2024-11-12 18:59:00 +00:00
fDirection += Up
2024-11-12 17:51:04 +00:00
else:
2024-11-12 18:59:00 +00:00
fDirection += Down
2024-11-12 17:51:04 +00:00
return fDirection
func _on_timer_timeout():
canShot = true
func death():
2024-11-12 18:59:00 +00:00
$AnimationPlayer.play(death)
2024-11-12 17:51:04 +00:00
current_state = player_states.dead
velocity.x = 0
set_collision_layer_value(1, false)
set_collision_mask_value(1, false)
$Respawn.start()
func _on_respawn_timeout():
2024-11-12 18:59:00 +00:00
var main = get_tree().get_nodes_in_group(main)[0]
2024-11-12 17:51:04 +00:00
main.lives_p2 -= 1
main.respawnP2()
queue_free()