Skip to content

Commit

Permalink
Improved ball throwing in basketball scene
Browse files Browse the repository at this point in the history
closed #219
  • Loading branch information
cbcerquiaga committed Aug 30, 2019
1 parent d458396 commit 89c7ca2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 deletions.
51 changes: 30 additions & 21 deletions You, Me, and the End of the World/Scripts/basketballLiteralBall.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,58 @@ extends RigidBody2D

onready var isHeld #if the ball is held, it can be thrown. If not, it's free
var holdingPlayer
var isGoingRight

const minPower = 22
const maxPower = 107
const minPower = 500
const maxPower = 1000
const goalPower = 798 #tested that this is the best power for getting the ball in the hoop most of the time

# Called when the node enters the scene tree for the first time.
func _ready():
self.set_bounce(.7)
self.set_bounce(.6)
self.mass = 1.2
self.friction = 1
isHeld = false
pass

func shoot_ball():
var mousePos = get_local_mouse_position()
var trueMousePos = mousePos
var selfPos = self.get_global_position()
# var power = Vector2(selfPos.x - mousePos.x, selfPos.y - mousePos.y)
# power.x = power.x/12
# power.y = power.y/6
# if power.y < minPower:
# power.y = minPower
# elif power.y > maxPower:
# power.y = maxPower
# if power.x < -maxPower:
# power.x = -maxPower
# elif power.x > maxPower:
# power.x = maxPower
var power = Vector2(1, 0)
var power = abs(selfPos.distance_to(mousePos))
power = (power + goalPower)/2 #normalize power
if power > maxPower:
power = maxPower
elif power < minPower:
power = minPower
#add spread to mouse position
var randSpread = int(rand_range(-holdingPlayer.spread,holdingPlayer.spread))
mousePos.x += randSpread
randSpread = int(rand_range(-holdingPlayer.spread,holdingPlayer.spread))
mousePos.y += randSpread
#find angle
var angle = selfPos.angle_to(mousePos)
var offset = Vector2(cos(angle), sin(angle))
self.look_at(trueMousePos)
# var angle = selfPos.angle_to(mousePos)
# var offset = Vector2(cos(angle), sin(angle))
#launch the ball
isHeld = false
holdingPlayer.canCatchBall = false
print("offset: " + str(offset) + " power: " + str(power))
apply_impulse(Vector2(0,0), power)
print(" power: " + str(power))
apply_impulse(Vector2(mousePos.x,mousePos.y),Vector2 (0, power).rotated(rotation))
set_applied_torque(0)

func timeout():
rotation = 0

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
func _physics_process(delta):
if isHeld:
self.position.x = holdingPlayer.holdArea.global_position.x
self.position.y = holdingPlayer.holdArea.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 All @@ -58,7 +63,11 @@ func _process(delta):
#isHeld = false
#if held but not clicked:
#snap to position for heldPlayer
#else:
else:
#check if bounced
if contacts_reported > 0:
if holdingPlayer != null:
holdingPlayer.canHoldBall = true
#check if close enough to be held
#if yes, set held to be true
pass
8 changes: 2 additions & 6 deletions You, Me, and the End of the World/Scripts/basketballPlayer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var maxStamina = 100 #the maximum stamina the player can have with their current
var staminaRegen = .15 #how quickly the player's stamina regenerates
var agility
var jump
var spread = 15
var spread = 5
var lastFrameEndSpeed = Vector2(0,0)


Expand Down Expand Up @@ -91,13 +91,9 @@ func _physics_process(delta):
if is_on_floor():
on_air_time = 0
if crouch:
print("DUCK!")
stamina -= staminaRegen/2 #stamina regenerates at half speed
#TODO: play crouch animation
#TODO: reduce hitbox size
pass
else:
if crouch:
print("cannonball!")
velocity.y += CROUCH_GRAVITY

if jumping and velocity.y > 0:
Expand Down
2 changes: 1 addition & 1 deletion You, Me, and the End of the World/Scripts/puck.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func _ready():
#set_contact_monitor(true)
set_use_custom_integrator(false)

func _process(delta):
func _physics_process(delta):
if contacts_reported > 0:
linear_velocity.bounce(linear_velocity)
print("contacts reported: " + str(contacts_reported))
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ scale = Vector2( 19.192, 9.603 )
tile_set = ExtResource( 3 )
cell_size = Vector2( 50, 50 )
format = 1
tile_data = PoolIntArray( 0, 0, 0, 20, 0, 0, 65536, 0, 0, 65556, 0, 0, 131072, 0, 0, 131092, 0, 0, 196608, 0, 0, 196628, 0, 0, 262144, 0, 0, 262164, 0, 0, 327680, 0, 0, 327700, 0, 0, 393216, 0, 0, 393236, 0, 0, 458752, 0, 0, 458772, 0, 0, 524288, 0, 0, 524308, 0, 0, 589824, 0, 0, 589844, 0, 0, 655360, 0, 0, 655380, 0, 0, 720896, 0, 0, 720897, 0, 0, 720898, 0, 0, 720899, 0, 0, 720900, 0, 0, 720901, 0, 0, 720902, 0, 0, 720903, 0, 0, 720904, 0, 0, 720905, 0, 0, 720906, 0, 0, 720907, 0, 0, 720908, 0, 0, 720909, 0, 0, 720910, 0, 0, 720911, 0, 0, 720912, 0, 0, 720913, 0, 0, 720914, 0, 0, 720915, 0, 0, 720916, 0, 0 )
tile_data = PoolIntArray( -524288, 0, 0, -524287, 0, 0, -524286, 0, 0, -524285, 0, 0, -524284, 0, 0, -524283, 0, 0, -524282, 0, 0, -524281, 0, 0, -524280, 0, 0, -524279, 0, 0, -524278, 0, 0, -524277, 0, 0, -524276, 0, 0, -524275, 0, 0, -524274, 0, 0, -524273, 0, 0, -524272, 0, 0, -524271, 0, 0, -524270, 0, 0, -524269, 0, 0, -524268, 0, 0, -458752, 0, 0, -458732, 0, 0, -393216, 0, 0, -393196, 0, 0, -327680, 0, 0, -327660, 0, 0, -262144, 0, 0, -262124, 0, 0, -196608, 0, 0, -196588, 0, 0, -131072, 0, 0, -131052, 0, 0, -65536, 0, 0, -65516, 0, 0, 0, 0, 0, 20, 0, 0, 65536, 0, 0, 65556, 0, 0, 131072, 0, 0, 131092, 0, 0, 196608, 0, 0, 196628, 0, 0, 262144, 0, 0, 262164, 0, 0, 327680, 0, 0, 327700, 0, 0, 393216, 0, 0, 393236, 0, 0, 458752, 0, 0, 458772, 0, 0, 524288, 0, 0, 524308, 0, 0, 589824, 0, 0, 589844, 0, 0, 655360, 0, 0, 655380, 0, 0, 720896, 0, 0, 720897, 0, 0, 720898, 0, 0, 720899, 0, 0, 720900, 0, 0, 720901, 0, 0, 720902, 0, 0, 720903, 0, 0, 720904, 0, 0, 720905, 0, 0, 720906, 0, 0, 720907, 0, 0, 720908, 0, 0, 720909, 0, 0, 720910, 0, 0, 720911, 0, 0, 720912, 0, 0, 720913, 0, 0, 720914, 0, 0, 720915, 0, 0, 720916, 0, 0 )

[node name="left hoop" type="RigidBody2D" parent="TileMap"]
position = Vector2( 100, 220 )
Expand Down

0 comments on commit 89c7ca2

Please sign in to comment.