Skip to content

Commit

Permalink
add multi ray casting
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTOXIN committed May 4, 2022
1 parent 83d5d81 commit 40c9f1b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
5 changes: 3 additions & 2 deletions bullets/Bullet.gd
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func explode():
$Explosion.play("smoke")

func seek():
#same operations
#same operations TODO HMMMMMMMMMMMMM
var desired: Vector2 = (target.position - position).normalized() * speed
var steer: Vector2 = velocity.direction_to(desired) * steer_force
return steer
Expand Down Expand Up @@ -55,7 +55,8 @@ func _on_Lifetime_timeout():
func _on_Explosion_animation_finished():
queue_free()

#dirty hack for area bullet damage
#udp: its not working when multiply bullets with big collision :(
func _on_Bullet_area_entered(area):
#dirty hack for ger area bullet damage
if area.has_method('take_damage'):
area.take_damage(damage)
2 changes: 1 addition & 1 deletion maps/Map.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extends Node2D
#TODO EXPLODE BULLETS BY BULLETS

func _ready():
set_camera_limits()
var cursor = load("res://assets/UI/crossair_white.png")
Expand Down
3 changes: 3 additions & 0 deletions maps/Map01.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ rotation = 3.11802

[node name="Enemy" parent="Paths/Path2D/PathFollow2D" instance=ExtResource( 4 )]
offroad_friction = 0.0
disable = true
detect_radius = 400

[node name="HUD" parent="." instance=ExtResource( 5 )]
Expand All @@ -49,6 +50,7 @@ detect_radius = 400
position = Vector2( 863, 365 )
gun_shots = 3
gun_spread = 0.1
disable = true

[node name="EnemyMissile" parent="." instance=ExtResource( 4 )]
position = Vector2( 539, 243 )
Expand All @@ -57,6 +59,7 @@ Bullet = ExtResource( 8 )
gun_cooldown = 1.5
offroad_friction = 0.0
max_ammo = 10
disable = true
detect_radius = 250

[node name="Obstacles" type="Node2D" parent="."]
Expand Down
42 changes: 27 additions & 15 deletions tanks/Enemy.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export (float) var accuracy_shoot = 0.9

onready var parent = get_parent()

var hit_pos: Vector2 = Vector2.ZERO
var target: Node2D = null
var hit_pos: Array = []
var speed = 0

func _ready():
Expand All @@ -19,27 +19,39 @@ func _draw():
if Globals.debag_mode:
draw_circle(Vector2.ZERO, detect_radius, Color(0, 0, 0, 0.25))
if target:
var v = (hit_pos - global_position).rotated(-global_rotation)
draw_circle(v, 5, Color.red)
draw_line(Vector2.ZERO, v, Color.red, 3)
for hit in hit_pos:
var v = (hit - global_position).rotated(-global_rotation)
draw_circle(v, 5, Color.red)
draw_line(Vector2.ZERO, v, Color.red, 3)

func _process(delta):
if !target or disable:
return

var result = get_world_2d().direct_space_state.intersect_ray(
global_position, target.global_position, [self], collision_mask
)

hit_pos = []

var space_state = get_world_2d().direct_space_state;
var collision_shape = target.get_node('CollisionShape2D')

var target_extents = collision_shape.shape.extents - Vector2(5, 5)
var shape_rotate = collision_shape.global_rotation

if result:
hit_pos = result.position
if result.collider.name == "Player":
targeting(delta)

var nw = target.position - target_extents.rotated(shape_rotate)
var se = target.position + target_extents.rotated(shape_rotate)
var ne = target.position + Vector2(target_extents.x, -target_extents.y).rotated(shape_rotate)
var sw = target.position + Vector2(-target_extents.x, target_extents.y).rotated(shape_rotate)

for target_pos in [target.position, nw, se, ne, sw]:
var result = space_state.intersect_ray(position, target_pos, [self], collision_mask)
if result:
hit_pos.append(result.position)
if result.collider.name == "Player":
targeting(target_pos, delta)
break #oprimze ray cast
update()

func targeting(delta):
var target_dir = (target.global_position - global_position).normalized()
func targeting(target_pos, delta):
var target_dir = (target_pos - global_position).normalized()
var current_dir = Vector2.RIGHT.rotated($Turret.global_rotation)
##
var v = current_dir.linear_interpolate(target_dir, turret_speed * delta)
Expand Down

0 comments on commit 40c9f1b

Please sign in to comment.