added the pigs

This commit is contained in:
Haze Weathers 2023-03-22 02:06:09 -04:00
parent 708d89e5be
commit 0a5c111615
3 changed files with 224 additions and 11 deletions

86
objects/enemy/cop.gd Normal file
View file

@ -0,0 +1,86 @@
extends "res://objects/enemy/enemy.gd"
export var walk_speed = 25.0
export var left_boundary = 0.0
export var right_boundary = 0.0
export var direction = 1.0
export var idle_turns = 4
export var turn_time = 0.5
export var shoot_time = 1.0
onready var sprite = $AnimatedSprite
onready var shoot_position = $"%ShootPosition"
onready var shoot_cast = $"%ShootCast"
onready var graphics_cast = $"%GraphicsCast"
onready var shoot_line = $"%ShootLine"
onready var sparks = $SparkParticles
var shooting = false
var turns = 0
func _ready():
# convert boundaries into actual coordinate positions
left_boundary = position.x - left_boundary * 8.0
right_boundary = position.x + right_boundary * 8.0
# make facing match direction
sprite.scale.x = direction
# make animation speed sync to walk speed
sprite.speed_scale = inverse_lerp(0.0, 25.0, walk_speed)
func _physics_process(delta):
if !shooting:
# check for player in raycast
var collider = shoot_cast.get_collider()
if collider != null && collider.is_in_group("player"):
# kill player and enter shooting state temporarily
collider.get_parent().die()
shooting = true
get_tree().create_timer(0.05, false).connect("timeout", self, "_stop_shoot")
sprite.play("shoot")
# check other raycast to find collision passing through player
graphics_cast.force_raycast_update()
var hit_position = graphics_cast.to_global(Vector2(256.0, 0.0))
if graphics_cast.is_colliding():
hit_position = graphics_cast.get_collision_point()
# set line point to point of collision
var hit_x = shoot_line.to_local(hit_position).x
shoot_line.points[1].x = hit_x
# shoot_line.visible = true
# get_tree().create_timer(0.05, false).connect("timeout", shoot_line, "set_visible", [false], CONNECT_DEFERRED)
# move and play sparks
sparks.global_position = hit_position
sparks.emitting = true
return
# if there aren't turns, walk around
if turns == 0:
sprite.play("walk")
position.x += direction * walk_speed * delta
if position.x <= left_boundary:
position.x = left_boundary
direction = 1.0
_do_turn()
elif position.x >= right_boundary:
position.x = right_boundary
direction = -1.0
_do_turn()
func _do_turn():
if shooting:
get_tree().create_timer(turn_time, false).connect("timeout", self, "_do_turn")
return
sprite.play("idle")
sprite.scale.x *= -1.0
if turns < idle_turns * 2:
turns += 1
get_tree().create_timer(turn_time, false).connect("timeout", self, "_do_turn")
else:
turns = 0
func _stop_shoot():
shooting = false
shoot_line.visible = false
if turns == 0:
sprite.play("walk")
else:
sprite.play("idle")

127
objects/enemy/cop.tscn Normal file
View file

@ -0,0 +1,127 @@
[gd_scene load_steps=17 format=2]
[ext_resource path="res://objects/enemy/cop.gd" type="Script" id=1]
[ext_resource path="res://graphics/enemy/cop/cop_idle.png" type="Texture" id=2]
[ext_resource path="res://graphics/enemy/cop/cop_walk.png" type="Texture" id=3]
[ext_resource path="res://graphics/enemy/cop/cop_shoot.png" type="Texture" id=4]
[ext_resource path="res://shaders/1px_border.gdshader" type="Shader" id=5]
[ext_resource path="res://graphics/particles/dust.png" type="Texture" id=6]
[sub_resource type="ShaderMaterial" id=7]
shader = ExtResource( 5 )
shader_param/border_color = Color( 0, 0, 0, 1 )
shader_param/border_corners = true
[sub_resource type="AtlasTexture" id=1]
atlas = ExtResource( 2 )
region = Rect2( 0, 0, 20, 21 )
[sub_resource type="AtlasTexture" id=2]
atlas = ExtResource( 3 )
region = Rect2( 0, 0, 20, 21 )
[sub_resource type="AtlasTexture" id=3]
atlas = ExtResource( 3 )
region = Rect2( 20, 0, 20, 21 )
[sub_resource type="AtlasTexture" id=4]
atlas = ExtResource( 3 )
region = Rect2( 40, 0, 20, 21 )
[sub_resource type="AtlasTexture" id=5]
atlas = ExtResource( 3 )
region = Rect2( 60, 0, 20, 21 )
[sub_resource type="SpriteFrames" id=6]
animations = [ {
"frames": [ SubResource( 1 ) ],
"loop": false,
"name": "idle",
"speed": 5.0
}, {
"frames": [ ExtResource( 4 ) ],
"loop": false,
"name": "shoot",
"speed": 1.0
}, {
"frames": [ SubResource( 2 ), SubResource( 3 ), SubResource( 4 ), SubResource( 5 ) ],
"loop": true,
"name": "walk",
"speed": 5.0
} ]
[sub_resource type="RectangleShape2D" id=8]
extents = Vector2( 3, 8 )
[sub_resource type="Curve" id=9]
_data = [ Vector2( 0, 1 ), 0.0, -1.45746, 0, 0, Vector2( 1, 0 ), 0.00323196, 0.0, 0, 0 ]
[sub_resource type="Gradient" id=10]
offsets = PoolRealArray( 0, 0.515152 )
colors = PoolColorArray( 1, 1, 1, 1, 1, 1, 0.290196, 1 )
[node name="Cop" type="Node2D"]
script = ExtResource( 1 )
idle_turns = 2
[node name="AnimatedSprite" type="AnimatedSprite" parent="."]
material = SubResource( 7 )
position = Vector2( 5, -2 )
frames = SubResource( 6 )
animation = "shoot"
playing = true
[node name="ShootPosition" type="Position2D" parent="AnimatedSprite"]
unique_name_in_owner = true
position = Vector2( 7, 0.5 )
__meta__ = {
"_gizmo_extents_": 4.0
}
[node name="ShootCast" type="RayCast2D" parent="AnimatedSprite/ShootPosition"]
unique_name_in_owner = true
visible = false
enabled = true
cast_to = Vector2( 256, 0 )
collision_mask = 8
collide_with_areas = true
[node name="GraphicsCast" type="RayCast2D" parent="AnimatedSprite/ShootPosition"]
unique_name_in_owner = true
visible = false
cast_to = Vector2( 256, 0 )
collision_mask = 8
[node name="ShootLine" type="Line2D" parent="AnimatedSprite/ShootPosition"]
unique_name_in_owner = true
visible = false
points = PoolVector2Array( 0, 0, 8, 0 )
width = 1.0
default_color = Color( 1, 1, 0.290196, 1 )
[node name="Hitbox" type="Area2D" parent="." groups=["enemy_hitbox"]]
[node name="CollisionShape2D" type="CollisionShape2D" parent="Hitbox"]
position = Vector2( 4, 0 )
shape = SubResource( 8 )
[node name="SparkParticles" type="CPUParticles2D" parent="."]
position = Vector2( 4, 0 )
emitting = false
amount = 16
lifetime = 0.5
one_shot = true
explosiveness = 1.0
texture = ExtResource( 6 )
spread = 180.0
gravity = Vector2( 0, 0 )
initial_velocity = 30.0
initial_velocity_random = 0.5
damping = 50.0
angle = 720.0
angle_random = 1.0
scale_amount = 0.5
scale_amount_curve = SubResource( 9 )
color_ramp = SubResource( 10 )
[connection signal="area_entered" from="Hitbox" to="." method="_on_Hitbox_area_entered"]