Skip to content

Commit

Permalink
Worked on bullet collisions in hunting scene
Browse files Browse the repository at this point in the history
  • Loading branch information
cbcerquiaga committed Mar 12, 2019
1 parent cb90710 commit eb346a5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
8 changes: 7 additions & 1 deletion You, Me, and the End of the World/Scripts/Hunting.gd
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func createBullet(playerPosition):

func _process(delta):
creature.setHunterLocation(player.position)
passBulletsInfo()
if creature.health <= 0:
print("You got it!")
#randomize what the player actually gets
Expand Down Expand Up @@ -68,4 +69,9 @@ func getXDist():
return player.position.x - creature.position.x

func getYDist():
return player.position.y - creature.position.y
return player.position.y - creature.position.y

func passBulletsInfo():
if player.bulletsFired.size() > 0:
for i in range(0, player.bulletsFired.size() - 1):
player.bulletsFired[i].loadCreature(creature)
27 changes: 26 additions & 1 deletion You, Me, and the End of the World/Scripts/HuntingBullet.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ var damage = 100 #default damage
var critChance = .2 #default critical chance
var speed = 500 #default speed 500
var motion = Vector2(2,0)
var distance = 0
var maxDistance = 1000

onready var mySprite = get_node("Sprite")
onready var creature = self.get_parent().get_node("Creature")
onready var creatureSprite = self.get_parent().get_node("Creature/Sprite")
onready var creatureSprite = load("res://Images/grandPiano.png")

signal hit
signal hitUpdate
Expand All @@ -27,17 +30,39 @@ func _process(delta):
#print(str(collideCheck))
if(collideCheck != null):
contact(collideCheck.collider)
var collideCheck2 = collideByPosition()
if collideCheck2:
print(str(collideCheck2))
# if self.position.x > creature.position.x - creatureSprite.texture.get_width() and self.position.x < creature.position.x + creatureSprite.texture.get_width():
# if self.position.y < creature.position.y + creatureSprite.texture.get_height() and self.position.y > creature.position.y - creatureSprite.texture.get_height():
# contact(creature)
distance = distance + 1
pass

func loadCreatureSprite(res):
creatureSprite = load(res)

func contact(body):
print("Hit creature")
emit_signal("hit", body.name, damage, critChance)
emit_signal("hitUpdate")
#if ricochet < 1:
destroy()

func collideByPosition():
var r1from = mySprite.position
var r1to = Vector2()
r1to.x = r1from.x + mySprite.texture.get_width()
r1to.y = r1from.y + mySprite.texture.get_height()
var r2from = creature.position
var r2to = Vector2()
r2to.x = r2from.x + creatureSprite.get_width()
r2to.y = r2from.y + creatureSprite.get_height()
#thanks, kifhan from reddit
return !(r2from.x >= r1to.x or r2to.x <= r1from.x or r2from.y >= r1to.y or r2to.y <= r1from.y)

func loadCreature(newCreature):
creature = newCreature

func destroy():
#play explosion animation and sound
Expand Down
9 changes: 9 additions & 0 deletions You, Me, and the End of the World/Scripts/HuntingPlayer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ onready var lostVal = 0
onready var spookVal = 0
onready var shotVal = 0#100
var spread = 20
onready var bulletsFired = []
onready var bullet = load("res://tscn files/HuntingBullet.tscn")

func _ready():
Expand All @@ -28,6 +29,13 @@ func create_bullet(position):
truePosition.y += randSpread
tempBullet.motion = truePosition.normalized()
self.get_parent().add_child(tempBullet)
bulletsFired.append(tempBullet)

func checkAndKillBullets():
for i in range(0, bulletsFired.size() - 1):
var tempBullet = bulletsFired[i]
if tempBullet.distance > tempBullet.maxDistance:
tempBullet.destroy()

func _process(delta):
if lostVal < 0:
Expand All @@ -53,4 +61,5 @@ func _process(delta):
shotVal = 0
create_bullet(self.position)
move_and_slide(velocity)
checkAndKillBullets()
pass

0 comments on commit eb346a5

Please sign in to comment.