From b353f478af16c79f3886d664f8d8f9c3dc1e84ab Mon Sep 17 00:00:00 2001 From: Blake Erquiaga Date: Fri, 30 Aug 2019 12:00:08 -0400 Subject: [PATCH] Added player2 to basketball scene #208 player2's throwing is still wonky, but the 2 players can play catch --- .../Scripts/Basketball.gd | 20 ++- .../Scripts/basketballLiteralBall.gd | 14 +- .../Scripts/basketballPlayer.gd | 5 +- .../Scripts/basketballPlayer2.gd | 142 ++++++++++++++++++ .../tscn files/Basketball.tscn | 7 +- .../tscn files/basketballPlayer.tscn | 18 ++- 6 files changed, 191 insertions(+), 15 deletions(-) create mode 100644 You, Me, and the End of the World/Scripts/basketballPlayer2.gd diff --git a/You, Me, and the End of the World/Scripts/Basketball.gd b/You, Me, and the End of the World/Scripts/Basketball.gd index 2fc3130..a4b47ea 100644 --- a/You, Me, and the End of the World/Scripts/Basketball.gd +++ b/You, Me, and the End of the World/Scripts/Basketball.gd @@ -8,7 +8,7 @@ onready var ball = get_node("TileMap/ball") onready var leftNet = get_node("TileMap/left hoop/net") onready var rightNet = get_node("TileMap/right hoop/net") onready var player1 = get_node("TileMap/player1") -#onready var playe2 = get_node("TileMap/player2") +onready var player2 = get_node("TileMap/player2") #onready var ai1 = get_node("TileMap/ai1") #onready var ai2 = get_node("TileMap/ai2") @@ -31,6 +31,9 @@ var alreadyScored = false #this will let us have a complete ball passing through # Called when the node enters the scene tree for the first time. func _ready(): Input.set_custom_mouse_cursor(hoopMouse) + #make the players fac the right way + player1.updateFacingRight(false) + player2.updateFacingRight(true) #flip a coin to see who shoots first #start the game pass @@ -46,6 +49,21 @@ func _process(delta): #check who can catch the ball if player1.catchArea.overlaps_body(ball) and player1.canCatchBall: if !ball.isHeld: + if ball.holdingPlayer != null: + ball.holdingPlayer.canCatchBall = true ball.isHeld = true ball.holdingPlayer = player1 + if player2.catchArea.overlaps_body(ball) and player2.canCatchBall: + if !ball.isHeld: + if ball.holdingPlayer != null: + ball.holdingPlayer.canCatchBall = true + ball.isHeld = true + ball.holdingPlayer = player2 + + #left team 1 turn, right team 1 turn, left team 2 turn, right team 2 turn + #shot clock goes + #if shot clock doesn't expire, ball is shot or stolen + #live ball timer goes + #next turn + #repeat pass diff --git a/You, Me, and the End of the World/Scripts/basketballLiteralBall.gd b/You, Me, and the End of the World/Scripts/basketballLiteralBall.gd index 7a12b82..7a5e7bb 100644 --- a/You, Me, and the End of the World/Scripts/basketballLiteralBall.gd +++ b/You, Me, and the End of the World/Scripts/basketballLiteralBall.gd @@ -11,8 +11,9 @@ const goalPower = 798 #tested that this is the best power for getting the ball i # Called when the node enters the scene tree for the first time. func _ready(): self.set_bounce(.6) - self.mass = 1.2 - self.friction = 1 + self.set_mass(1.2) + self.set_friction(1) + mode = RigidBody2D.MODE_RIGID isHeld = false pass @@ -40,20 +41,21 @@ func shoot_ball(): holdingPlayer.canCatchBall = false print(" power: " + str(power)) apply_impulse(Vector2(mousePos.x,mousePos.y),Vector2 (0, power).rotated(rotation)) - set_applied_torque(0) + self.set_inertia(2000000) func timeout(): + print("you're in time out") rotation = 0 + set_applied_torque(0) # Called every frame. 'delta' is the elapsed time since the previous frame. func _physics_process(delta): if isHeld: - self.position.x = holdingPlayer.holdArea.global_position.x - self.position.y = holdingPlayer.holdArea.global_position.y + self.position.x = holdingPlayer.currentHoldArea.global_position.x + self.position.y = holdingPlayer.currentHoldArea.global_position.y if Input.is_action_just_pressed("click") and !holdingPlayer.isMouseNull: shoot_ball() #short delay, then stop spinning - yield(get_tree().create_timer(0.5), "timeout") #if mouse clicked and isHeld: #check mouse position #get distance from ball diff --git a/You, Me, and the End of the World/Scripts/basketballPlayer.gd b/You, Me, and the End of the World/Scripts/basketballPlayer.gd index 13a6b6a..d1ca01c 100644 --- a/You, Me, and the End of the World/Scripts/basketballPlayer.gd +++ b/You, Me, and the End of the World/Scripts/basketballPlayer.gd @@ -17,7 +17,7 @@ const SLIDE_STOP_VELOCITY = 1.0 # one pixel/second const SLIDE_STOP_MIN_TRAVEL = 1.0 # one pixel onready var catchArea = get_node("catchArea") -onready var holdArea = get_node("holdArea") +onready var currentHoldArea = get_node("holdAreaL") var isMouseNull = false @@ -128,8 +128,11 @@ func updateFacingRight(boolean): isFacingRight = boolean if isFacingRight: get_node("Sprite").set_flip_h(false) + currentHoldArea = get_node("holdAreaL") else: get_node("Sprite").set_flip_h(true) + currentHoldArea = get_node("holdAreaR") + func freeze(): frozen = true diff --git a/You, Me, and the End of the World/Scripts/basketballPlayer2.gd b/You, Me, and the End of the World/Scripts/basketballPlayer2.gd new file mode 100644 index 0000000..5ce2780 --- /dev/null +++ b/You, Me, and the End of the World/Scripts/basketballPlayer2.gd @@ -0,0 +1,142 @@ +extends KinematicBody2D + +# Member variables +const GRAVITY = 500.0 # pixels/second/second +const CROUCH_GRAVITY = 120.0 #pixels/second/second + +# Angle in degrees towards either side that the player can consider "floor" +const FLOOR_ANGLE_TOLERANCE = 40 +const WALK_FORCE = 900 +const WALK_MIN_SPEED = 10 +const WALK_MAX_SPEED = 200 +const STOP_FORCE = 1300 +const JUMP_SPEED = 400 +const JUMP_MAX_AIRBORNE_TIME = .4 + +const SLIDE_STOP_VELOCITY = 1.0 # one pixel/second +const SLIDE_STOP_MIN_TRAVEL = 1.0 # one pixel + +onready var catchArea = get_node("catchArea") +onready var currentHoldArea = get_node("holdAreaL") + +var isMouseNull = false + +var velocity = Vector2() +var on_air_time = 100 +var jumping = false + +var prev_jump_pressed = false + +onready var frozen = false + +#basketball-affecting variables +var isFacingRight = false +var canCatchBall = true #turned off when the player shoots the ball, turned back on later + +#player status variables +var stamina = 100 #the player's current stamina level +var maxStamina = 100 #the maximum stamina the player can have with their current fitness +var staminaRegen = .15 #how quickly the player's stamina regenerates +var agility +var jump +var spread = 5 +var lastFrameEndSpeed = Vector2(0,0) + + +func _ready(): + #ready stuff + pass + +func _physics_process(delta): + if !frozen: + + # Create forces + var force = Vector2(0, GRAVITY) + + var walk_left = Input.is_action_pressed("p2_move_left") + var walk_right = Input.is_action_pressed("p2_move_right") + var block = Input.is_action_just_pressed("p2_action1") + var steal = Input.is_action_just_pressed("p2_action2") + var jump = Input.is_action_pressed("p2_move_up") + var crouch = Input.is_action_pressed("p2_move_down") + + + var stop = true + + if walk_left: + if velocity.x <= WALK_MIN_SPEED and velocity.x > -WALK_MAX_SPEED: + force.x -= WALK_FORCE + stop = false + elif walk_right: + if velocity.x >= -WALK_MIN_SPEED and velocity.x < WALK_MAX_SPEED: + force.x += WALK_FORCE + stop = false + + if stop: + var vsign = sign(velocity.x) + var vlen = abs(velocity.x) + + vlen -= STOP_FORCE * delta + if vlen < 0: + vlen = 0 + + velocity.x = vlen * vsign + + # Integrate forces to velocity + velocity += force * delta + # Integrate velocity into motion and move + velocity = move_and_slide(velocity, Vector2(0, -1)) + lastFrameEndSpeed = velocity + + if is_on_floor(): + on_air_time = 0 + if crouch: + pass + else: + if crouch: + velocity.y += CROUCH_GRAVITY + + if jumping and velocity.y > 0: + # If falling, no longer jumping + jumping = false + + if on_air_time < JUMP_MAX_AIRBORNE_TIME and jump and not prev_jump_pressed and not jumping and stamina > 20: + # Jump must also be allowed to happen if the character left the floor a little bit ago. + # Makes controls more snappy. + velocity.y = -JUMP_SPEED + jumping = true + stamina -= 20 + + on_air_time += delta + prev_jump_pressed = jump + if stamina < maxStamina: + stamina += staminaRegen + pass + + +func _on_Border_mouse_entered(): + isMouseNull = false +# print("Mouse is not null") + + +func _on_Border_mouse_exited(): + isMouseNull = true +# print("Mouse is null") + + +func updateFacingRight(boolean): + isFacingRight = boolean + if isFacingRight: + get_node("Sprite").set_flip_h(false) + currentHoldArea = get_node("holdAreaL") + else: + get_node("Sprite").set_flip_h(true) + currentHoldArea = get_node("holdAreaR") + +func freeze(): + frozen = true + position.x = -6000 + +func unfreeze(): + frozen = false + position.x = 100 \ No newline at end of file diff --git a/You, Me, and the End of the World/tscn files/Basketball.tscn b/You, Me, and the End of the World/tscn files/Basketball.tscn index bace5a5..a268c27 100644 --- a/You, Me, and the End of the World/tscn files/Basketball.tscn +++ b/You, Me, and the End of the World/tscn files/Basketball.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=11 format=2] +[gd_scene load_steps=12 format=2] [ext_resource path="res://Scripts/Basketball.gd" type="Script" id=1] [ext_resource path="res://tscn files/pause_popup.tscn" type="PackedScene" id=2] @@ -7,6 +7,7 @@ [ext_resource path="res://Scripts/basketballLiteralBall.gd" type="Script" id=5] [ext_resource path="res://Images/basketball.png" type="Texture" id=6] [ext_resource path="res://tscn files/basketballPlayer.tscn" type="PackedScene" id=7] +[ext_resource path="res://Scripts/basketballPlayer2.gd" type="Script" id=8] [sub_resource type="RectangleShape2D" id=1] extents = Vector2( 43.9323, 2.07031 ) @@ -103,3 +104,7 @@ shape = SubResource( 3 ) [node name="player1" parent="TileMap" instance=ExtResource( 7 )] position = Vector2( 167.168, 469.402 ) +[node name="player2" parent="TileMap" instance=ExtResource( 7 )] +position = Vector2( 800, 469.402 ) +script = ExtResource( 8 ) + diff --git a/You, Me, and the End of the World/tscn files/basketballPlayer.tscn b/You, Me, and the End of the World/tscn files/basketballPlayer.tscn index 5b13257..a7c39c1 100644 --- a/You, Me, and the End of the World/tscn files/basketballPlayer.tscn +++ b/You, Me, and the End of the World/tscn files/basketballPlayer.tscn @@ -6,9 +6,9 @@ [sub_resource type="RectangleShape2D" id=1] extents = Vector2( 30.4734, 45.053 ) -[sub_resource type="CircleShape2D" id=3] - [sub_resource type="CircleShape2D" id=2] + +[sub_resource type="CircleShape2D" id=3] radius = 63.0136 [node name="Node2D" type="KinematicBody2D"] @@ -20,14 +20,20 @@ shape = SubResource( 1 ) [node name="Sprite" type="Sprite" parent="."] texture = ExtResource( 2 ) -[node name="holdArea" type="Area2D" parent="."] +[node name="holdAreaR" type="Area2D" parent="."] position = Vector2( 39, 0 ) -[node name="CollisionShape2D" type="CollisionShape2D" parent="holdArea"] -shape = SubResource( 3 ) +[node name="CollisionShape2D" type="CollisionShape2D" parent="holdAreaR"] +shape = SubResource( 2 ) + +[node name="holdAreaL" type="Area2D" parent="."] +position = Vector2( -39, 0 ) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="holdAreaL"] +shape = SubResource( 2 ) [node name="catchArea" type="Area2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="catchArea"] -shape = SubResource( 2 ) +shape = SubResource( 3 )