Skip to content

Commit

Permalink
Added player2 to basketball scene
Browse files Browse the repository at this point in the history
#208 player2's throwing is still wonky, but the 2 players can play catch
  • Loading branch information
cbcerquiaga committed Aug 30, 2019
1 parent 89c7ca2 commit b353f47
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 15 deletions.
20 changes: 19 additions & 1 deletion You, Me, and the End of the World/Scripts/Basketball.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
142 changes: 142 additions & 0 deletions You, Me, and the End of the World/Scripts/basketballPlayer2.gd
Original file line number Diff line number Diff line change
@@ -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
7 changes: 6 additions & 1 deletion You, Me, and the End of the World/tscn files/Basketball.tscn
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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 )
Expand Down Expand Up @@ -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 )

18 changes: 12 additions & 6 deletions You, Me, and the End of the World/tscn files/basketballPlayer.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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 )

0 comments on commit b353f47

Please sign in to comment.