Major addition of comments, removed some redundant player code

This commit is contained in:
Sage Vaillancourt 2018-05-27 03:46:10 -04:00
parent d9e05e12df
commit d420f3706a
3 changed files with 115 additions and 73 deletions

View File

@ -27,7 +27,7 @@ animations = [ {
"speed": 5.0
} ]
[node name="Main" type="Node" index="0"]
[node name="Main" type="Node"]
script = ExtResource( 1 )
_sections_unfolded = [ "Pause" ]

184
Player.gd
View File

@ -1,6 +1,6 @@
extends Area2D
# signals
signal update_display # tells parent to update points display
signal refund # will tell parent a refund is occuring, for display
signal shooting_speed_upgrade # signals that a shooting speed upgrade has occured
@ -15,54 +15,87 @@ signal multiplayer_movement
export (PackedScene) var Laser
################################
#THINGS THAT MAY NEED ADJUSTING
# THINGS THAT MAY NEED ADJUSTING
#################################
#Default ship strengths and costs
# first 5 tiers of shooting speed
const BULLET_DELAY_TIER1 = 0.8
const BULLET_DELAY_TIER2 = 0.5
const BULLET_DELAY_TIER3 = 0.2
const BULLET_DELAY_TIER4 = 0.1
const BULLET_DELAY_TIER5 = 0.05
# starting cost of shooting speed upgrades after tier 5
const SHOOTING_SPEED_UPGRADE_DEFAULT = 1500
# can't shoot faster than this
const BULLET_DELAY_MINIMUM = 0.01
# costs of shooting speed upgrades
const BULLET_DELAY_TIER2_COST = 100
const BULLET_DELAY_TIER3_COST = 200
const BULLET_DELAY_TIER4_COST = 400
const BULLET_DELAY_TIER5_COST = 1000
# the 5 tiers of ship speed
const SHIP_SPEED_TIER1 = 150
const SHIP_SPEED_TIER2 = 200
const SHIP_SPEED_TIER3 = 300
const SHIP_SPEED_TIER4 = 500
const SHIP_SPEED_TIER5 = 800
const SHIP_SPEED_UPGRADE_DEFAULT = 1500
const SHIP_SPEED_MAXIMUM = 1200
# costs of ship speed upgrades
const SHIP_SPEED_TIER2_COST = 200
const SHIP_SPEED_TIER3_COST = 400
const SHIP_SPEED_TIER4_COST = 600
const SHIP_SPEED_TIER5_COST = 800
# the ship's starting position
const STARTING_POSITION = Vector2(100, 250)
# the amount of money the player starts with
const STARTING_MONEY = 100
#################################
# set sliding shooting speed upgrade costs
var shooting_speed_upgrade = SHOOTING_SPEED_UPGRADE_DEFAULT
# used to limit player movement
var screensize
# can the player shoot at this instant
var can_shoot = false
# timer for shooting speed
var timer = null
# sets ship movement speed to the default
var ship_speed = SHIP_SPEED_TIER1
var ship_speed_tier = 0
# sets the delay between shots to the default
var bullet_delay = BULLET_DELAY_TIER1
var bullet_delay_tier = 0
# sets other shooting settings to their defaults
var laser_penetration = 0
var double_laser = false
var ship_value = 0
var refund_percentage = 1
# gives the player their starting points
var money = STARTING_MONEY
var shooting_speed_upgrade = SHOOTING_SPEED_UPGRADE_DEFAULT
var ship_speed_upgrade = SHIP_SPEED_UPGRADE_DEFAULT
# sets initial value of ship, to be added to with upgrades for refunds
var ship_value = 0
# what percentage of points should be returned on refunding
var refund_percentage = 1
# is a menu open? try to ignore input in-game
# only really relevant to touchscreens
var menu_open = false
# opens the upgrade menu
func upgradeMenu():
menu_open = true
var upgmenu = preload("res://UpgradeMenu.tscn").instance()
@ -70,15 +103,36 @@ func upgradeMenu():
upgmenu.connect("refund", self, "_refund_button")
upgmenu.connect("bullet_delay_upgrade", self, "upgradeBulletDelay_button")
upgmenu.connect("ship_speed_upgrade", self, "upgradeShipSpeed_button")
upgmenu.connect("double_laser_upgrade", self, "doubleLaserUpgrade_button")
upgmenu.connect("laser_penetration_upgrade", self, "laserPenetrationUpgrade_button")
upgmenu.connect("menu_closed", self, "menuClosed")
upgmenu.bullet_delay_tier = bullet_delay_tier
upgmenu.ship_speed_tier = ship_speed_tier
if !get_tree().has_network_peer():
get_tree().paused = true
# signalled on close of UpgradeMenu
func menuClosed():
menu_open = false
#######################
# REQUESTING UPGRADES #
#######################
# all will request their upgrade
# all will pause the game if not in multiplayer
func doubleLaserUpgrade_button():
doubleLaserUpgrade()
emit_signal("update_display")
if !get_tree().has_network_peer():
get_tree().paused = true
func laserPenetrationUpgrade_button():
laserPenetrationUpgrade()
emit_signal("update_display")
if !get_tree().has_network_peer():
get_tree().paused = true
func upgradeShipSpeed_button():
upgradeShipSpeed()
emit_signal("update_display")
@ -91,6 +145,18 @@ func upgradeBulletDelay_button():
if !get_tree().has_network_peer():
get_tree().paused = true
#######################
######################
# PROVIDING UPGRADES #
######################
# checks which tier the player is already on, and if they have enough points
# upgrades and charges the player, if possible
# increases the ship value by the amount spent, for refunds
# sends an rpc message about current state for a given upgrade
# upgrades shooting speed
func upgradeBulletDelay():
if (bullet_delay == BULLET_DELAY_TIER1 && money >= BULLET_DELAY_TIER2_COST):
bullet_delay = BULLET_DELAY_TIER2
@ -120,11 +186,12 @@ func upgradeBulletDelay():
ship_value += shooting_speed_upgrade
shooting_speed_upgrade *= 1.1
bullet_delay_tier += 1
timer.set_wait_time(bullet_delay)
timer.set_wait_time(bullet_delay)
rpc("other_shooting_speed_upgrade", get_tree().get_network_unique_id(), bullet_delay)
prints("rpc other_shootng_speed_upgrade", get_tree().get_network_unique_id(), bullet_delay)
# upgrades the speed at which the ship moves
# not relevant for touchscreen users
func upgradeShipSpeed():
prints("func upgradeShipSpeed():")
if (ship_speed == SHIP_SPEED_TIER1 && money >= SHIP_SPEED_TIER2_COST):
@ -147,17 +214,13 @@ func upgradeShipSpeed():
money -= SHIP_SPEED_TIER5_COST
ship_value += SHIP_SPEED_TIER5_COST
ship_speed_tier = 4
elif (ship_speed >= SHIP_SPEED_MAXIMUM):
emit_signal("ship_speed_fully_upgraded")
elif (ship_speed >= SHIP_SPEED_TIER5 && money >= ship_speed_upgrade):
ship_speed = ship_speed*1.05
money -= ship_speed_upgrade
ship_value += ship_speed_upgrade
ship_speed_upgrade *= 1.1
prints(ship_speed)
######################
func _ready():
# for measuring time between lasers
timer = Timer.new()
timer.set_one_shot(true)
timer.set_wait_time(bullet_delay)
@ -165,17 +228,24 @@ func _ready():
add_child(timer)
timer.start()
position.x = 100
position.y = 250
# set player's position to the default
position = STARTING_POSITION
# sets screensize to prevent player leaving screen
screensize = get_viewport_rect().size
# starts the ship's rocketing animation
$AnimatedSprite.play()
# resets all upgrades to the default
# refunds them with adjustable percentage returned
func _refund_button():
bullet_delay = BULLET_DELAY_TIER1
bullet_delay_tier = 0
timer.set_wait_time(bullet_delay)
ship_speed = SHIP_SPEED_TIER1
laser_penetration = 0
double_laser = false
shooting_speed_upgrade = SHOOTING_SPEED_UPGRADE_DEFAULT
money += ship_value*refund_percentage
prints("Refunded ", ship_value*refund_percentage)
@ -183,45 +253,21 @@ func _refund_button():
emit_signal("update_display")
get_tree().paused = true
var timer = null
var can_shoot = false
var shoot_down = false
func on_timeout_complete():
can_shoot = true
#func _unhandled_input(event):
# if (event is InputEventScreenTouch):
# gravity_point = true
# gravity_vec = Vector2(0.5, 0.5)
var x_up = false
var y_up = false
var x_down = false
var y_down = false
var is_shooting
var menu_open = false
func moveto(finger_position):
if !menu_open:
if (finger_position.x > position.x && finger_position.x < 800):
velocity.x += 1
x_up = true
if position.x > finger_position.x:
velocity.x -= 1
x_down = true
if (finger_position.y - 100 > position.y && finger_position.x < 800):
velocity.y += 1
y_up = true
if (position.y > finger_position.y - 100 && finger_position.x < 800):
velocity.y -= 1
y_down = true
# if (finger_position.x >= 800 && can_shoot):
# shoot()
# if get_tree().has_network_peer():
# rpc("move_player", get_tree().get_network_unique_id(), position, ~can_shoot)
remote func move_player(id, position, is_shooting):
emit_signal("multiplayer_movement", id, position, is_shooting)
@ -229,56 +275,49 @@ remote func move_player(id, position, is_shooting):
remote func other_shooting_speed_upgrade(id, bullet_delay):
emit_signal("other_shooting_upgrade", id, bullet_delay)
var velocity = Vector2() # the player's movement vector
# the player's movement vector
var velocity = Vector2()
func _process(delta):
# move player with keyboard
velocity = Vector2()
if (Input.is_action_pressed("ui_right") || x_up):
if Input.is_action_pressed("ui_right"):
velocity.x += 1
x_up = false
if (Input.is_action_pressed("ui_left") || x_down):
if Input.is_action_pressed("ui_left"):
velocity.x -= 1
x_down = false
if (Input.is_action_pressed("ui_down") || y_up):
if Input.is_action_pressed("ui_down"):
velocity.y += 1
y_up = false
if (Input.is_action_pressed("ui_up") || y_down):
if Input.is_action_pressed("ui_up"):
velocity.y -= 1
y_down = false
if (velocity.length() > 0):
velocity = velocity.normalized() * ship_speed
position += velocity * delta
# prevents player leaving the screen
position.x = clamp(position.x, 0, screensize.x)
position.y = clamp(position.y, 0, screensize.y)
# basically exists just for rpc messages about whether or not the player is currently firing
if can_shoot:
is_shooting = false
else:
is_shooting = true
# if in multiplayer mode, push position and shooting status as often as possible
if get_tree().has_network_peer():
rpc_unreliable("move_player", get_tree().get_network_unique_id(), position, is_shooting)
if ((Input.is_action_pressed("ui_accept") || shoot_down == true) && can_shoot == true):
# if shoot button is pressed, try shooting
if Input.is_action_pressed("ui_accept"):
shoot()
if (Input.is_action_pressed("ui_reset") && can_shoot == true):
upgradeMenu()
var x_up = false
var y_up = false
var x_down = false
var y_down = false
func _on_Player_body_entered(body):
prints("hit")
func shootDown():
shoot_down = true
func shootUp():
shoot_down = false
# ui_reset button opens the upgrade menu
if Input.is_action_pressed("ui_reset"):
upgradeMenu()
# spawn lasers to shoot
# lasers can be set to penetrate or not
# can fire two lasers at once if double_laser is on
func shoot():
if can_shoot:
var laser = Laser.instance()
@ -296,10 +335,11 @@ func shoot():
laser2.position.x = position.x + 46
laser2.current_pen = laser_penetration
# don't shoot again until the timer resets
can_shoot = false
timer.start()
# displays endgame screen and pauses
func gameOver():
var gameover = preload("res://GameOver.tscn").instance()
add_child(gameover)
@ -307,6 +347,8 @@ func gameOver():
# gameover.connect("bullet_delay_upgrade", self, "upgradeBulletDelay_button")
get_tree().paused = true
# uses refund function to remove upgrades
# sets points back to default
func restart_game():
_refund_button()
get_tree().paused = false

View File

@ -20,7 +20,7 @@ custom_solver_bias = 0.0
radius = 12.8998
height = 40.4125
[node name="Player" type="Area2D"]
[node name="Player" type="Area2D" index="0"]
input_pickable = true
gravity_point = true