Skip to content

Commit

Permalink
2D laser demo
Browse files Browse the repository at this point in the history
  • Loading branch information
cbscribe committed Apr 4, 2019
1 parent f3d52aa commit ce62347
Show file tree
Hide file tree
Showing 15 changed files with 844 additions and 0 deletions.
51 changes: 51 additions & 0 deletions 2d_laser/Soldier.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
extends KinematicBody2D

var speed = 250
var velocity = Vector2()
var can_shoot = true
export var beam_duration = 1.5
export var cooldown = 0.5
var hit = null

func _ready():
$Line2D.remove_point(1)

func get_input():
velocity = Vector2()
if Input.is_action_pressed("forward"):
velocity += transform.x * speed
if Input.is_action_pressed("backward"):
velocity += -transform.x * speed
if Input.is_action_pressed("strafe_right"):
velocity += transform.y * speed/2
if Input.is_action_pressed("strafe_left"):
velocity += -transform.y * speed/2
if Input.is_action_just_pressed("click") and can_shoot:
shoot()

func shoot():
can_shoot = false
hit = cast_beam()
yield(get_tree().create_timer(beam_duration), "timeout")
$Line2D.remove_point(1)
hit = null
yield(get_tree().create_timer(cooldown), "timeout")
can_shoot = true

func cast_beam():
var space_state = get_world_2d().direct_space_state
var result = space_state.intersect_ray($Muzzle.global_position, $Muzzle.global_position + transform.x * 1000, [self])
if result:
if !hit:
$Line2D.add_point(transform.xform_inv(result.position))
else:
$Line2D.set_point_position(1, transform.xform_inv(result.position))
return result

func _physics_process(delta):
look_at(get_global_mouse_position())
get_input()
if position.distance_to(get_global_mouse_position()) > 25:
velocity = move_and_slide(velocity)
if hit:
hit = cast_beam()
36 changes: 36 additions & 0 deletions 2d_laser/Soldier.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[gd_scene load_steps=6 format=2]

[ext_resource path="res://Soldier.gd" type="Script" id=1]
[ext_resource path="res://assets/spritesheet_characters.png" type="Texture" id=2]
[ext_resource path="res://beam_pulse1.shader" type="Shader" id=3]

[sub_resource type="RectangleShape2D" id=1]
extents = Vector2( 17.3386, 26.1331 )

[sub_resource type="ShaderMaterial" id=2]
shader = ExtResource( 3 )

[node name="Soldier" type="KinematicBody2D"]
script = ExtResource( 1 )

[node name="Sprite" type="Sprite" parent="."]
texture = ExtResource( 2 )
region_enabled = true
region_rect = Rect2( 113, 0, 51, 43 )

[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
rotation = 1.5708
shape = SubResource( 1 )

[node name="Muzzle" type="Position2D" parent="."]
position = Vector2( 25, 9 )

[node name="Camera2D" type="Camera2D" parent="."]
current = true

[node name="Line2D" type="Line2D" parent="."]
material = SubResource( 2 )
points = PoolVector2Array( 25, 9, 231.473, 9.27283 )
default_color = Color( 2, 0.17, 0, 1 )
texture_mode = 101
end_cap_mode = 2
13 changes: 13 additions & 0 deletions 2d_laser/TileMap.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
extends TileMap

# Declare member variables here. Examples:
# var a = 2
# var b = "text"

# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.

# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass
Loading

0 comments on commit ce62347

Please sign in to comment.