diff options
-rw-r--r-- | assets/characters/base_character/BaseCharacter.gd | 15 | ||||
-rw-r--r-- | assets/characters/base_character/BaseCharacter.tscn | 4 | ||||
-rw-r--r-- | assets/view_model/ViewModel.gd | 14 | ||||
-rw-r--r-- | assets/weapons/goo_gun/Bullet.gd | 4 | ||||
-rw-r--r-- | assets/weapons/goo_gun/Bullet.tscn | 26 | ||||
-rw-r--r-- | assets/weapons/goo_gun/GooGun.gd | 29 | ||||
-rw-r--r-- | assets/weapons/goo_gun/GooGun.tscn | 4 |
7 files changed, 92 insertions, 4 deletions
diff --git a/assets/characters/base_character/BaseCharacter.gd b/assets/characters/base_character/BaseCharacter.gd new file mode 100644 index 0000000..bcc0f26 --- /dev/null +++ b/assets/characters/base_character/BaseCharacter.gd @@ -0,0 +1,15 @@ +extends KinematicBody + +export var is_firing = false +export var look_angle = 0 +export var weapon_pitch = 0 +export var weapon_pitch_bias = deg2rad(20) + +func _process(_delta): + var effective_weapon_pitch = weapon_pitch - weapon_pitch_bias if is_firing else weapon_pitch + effective_weapon_pitch = clamp(effective_weapon_pitch, deg2rad(-90), deg2rad(90)) + $GooGun.is_firing = is_firing + $GooGun.look_angle = look_angle + $GooGun.weapon_pitch = effective_weapon_pitch + $GooGun.rotation.x = effective_weapon_pitch + rotation.y = look_angle diff --git a/assets/characters/base_character/BaseCharacter.tscn b/assets/characters/base_character/BaseCharacter.tscn index 191e581..7b22f63 100644 --- a/assets/characters/base_character/BaseCharacter.tscn +++ b/assets/characters/base_character/BaseCharacter.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=5 format=2] +[gd_scene load_steps=6 format=2] [ext_resource path="res://assets/weapons/goo_gun/GooGun.tscn" type="PackedScene" id=1] [ext_resource path="res://assets/characters/base_character/friendly_material.tres" type="Material" id=2] +[ext_resource path="res://assets/characters/base_character/BaseCharacter.gd" type="Script" id=3] [sub_resource type="CapsuleMesh" id=1] material = ExtResource( 2 ) @@ -13,6 +14,7 @@ radius = 0.3 height = 0.9 [node name="BaseCharacter" type="KinematicBody"] +script = ExtResource( 3 ) [node name="Body" type="MeshInstance" parent="."] transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0, 0 ) diff --git a/assets/view_model/ViewModel.gd b/assets/view_model/ViewModel.gd index 7d5ce70..1c29a0e 100644 --- a/assets/view_model/ViewModel.gd +++ b/assets/view_model/ViewModel.gd @@ -46,10 +46,20 @@ func _physics_process(delta): # Move character. Param 4 (true) means stop on slopes velocity = character.move_and_slide_with_snap(velocity, snap, Vector3.UP, true) + # Update character look direction to face towards aiming if firing + if Input.is_action_pressed("fire"): + character.look_angle = spring_arm.rotation.y + character.weapon_pitch = -spring_arm.rotation.x # Update character look direction if moved - if move_direction.length() > 0.2: + elif move_direction.length() > 0.2: var look_direction = Vector2(-velocity.z, -velocity.x) - character.rotation.y = look_direction.angle() + character.look_angle = look_direction.angle() + character.weapon_pitch = 0 + else: + character.weapon_pitch = 0 + + # Handle extra player inputs + character.is_firing = Input.is_action_pressed("fire") func _unhandled_input(event): # Implement mouse aim direction diff --git a/assets/weapons/goo_gun/Bullet.gd b/assets/weapons/goo_gun/Bullet.gd new file mode 100644 index 0000000..1a4c01a --- /dev/null +++ b/assets/weapons/goo_gun/Bullet.gd @@ -0,0 +1,4 @@ +extends RigidBody + +func _on_Bullet_body_entered(_body): + queue_free() diff --git a/assets/weapons/goo_gun/Bullet.tscn b/assets/weapons/goo_gun/Bullet.tscn new file mode 100644 index 0000000..69ba3b6 --- /dev/null +++ b/assets/weapons/goo_gun/Bullet.tscn @@ -0,0 +1,26 @@ +[gd_scene load_steps=5 format=2] + +[ext_resource path="res://assets/characters/base_character/friendly_material.tres" type="Material" id=1] +[ext_resource path="res://assets/weapons/goo_gun/Bullet.gd" type="Script" id=2] + +[sub_resource type="SphereMesh" id=1] +material = ExtResource( 1 ) + +[sub_resource type="SphereShape" id=2] + +[node name="Bullet" type="RigidBody"] +transform = Transform( 0.3, 0, 0, 0, 0.3, 0, 0, 0, 0.3, 0, 0, 0 ) +gravity_scale = 5.0 +contacts_reported = 1 +contact_monitor = true +linear_damp = 2.0 +script = ExtResource( 2 ) + +[node name="MeshInstance" type="MeshInstance" parent="."] +mesh = SubResource( 1 ) +material/0 = null + +[node name="CollisionShape" type="CollisionShape" parent="."] +shape = SubResource( 2 ) + +[connection signal="body_entered" from="." to="." method="_on_Bullet_body_entered"] diff --git a/assets/weapons/goo_gun/GooGun.gd b/assets/weapons/goo_gun/GooGun.gd new file mode 100644 index 0000000..6a6758d --- /dev/null +++ b/assets/weapons/goo_gun/GooGun.gd @@ -0,0 +1,29 @@ +extends Spatial + +export var is_firing = false +export var fire_rate = 0.075 +export var projectile_spread = 0 #TODO +export var projectile_scalar = 40 +export var look_angle = 0 +export var weapon_pitch = 0 + +var time_since_fire = 0 + +var Bullet = preload("res://assets/weapons/goo_gun/Bullet.tscn") + +func _physics_process(delta): + if is_firing: + time_since_fire += delta + if time_since_fire >= fire_rate: + time_since_fire -= fire_rate + do_fire_projectile() + else: + time_since_fire = fire_rate + +func do_fire_projectile(): + var bullet = Bullet.instance() + $Nozzle.add_child(bullet) + bullet.set_as_toplevel(true) + var shoot_direction = Vector3.FORWARD.rotated(Vector3.LEFT, weapon_pitch) + shoot_direction = shoot_direction.rotated(Vector3.UP, look_angle) + bullet.apply_central_impulse(shoot_direction.normalized() * projectile_scalar) diff --git a/assets/weapons/goo_gun/GooGun.tscn b/assets/weapons/goo_gun/GooGun.tscn index f0b9622..edf2901 100644 --- a/assets/weapons/goo_gun/GooGun.tscn +++ b/assets/weapons/goo_gun/GooGun.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=7 format=2] +[gd_scene load_steps=8 format=2] [ext_resource path="res://assets/weapons/goo_gun/gun_material.tres" type="Material" id=1] [ext_resource path="res://assets/weapons/goo_gun/tank_material.tres" type="Material" id=2] +[ext_resource path="res://assets/weapons/goo_gun/GooGun.gd" type="Script" id=3] [sub_resource type="CubeMesh" id=1] material = ExtResource( 1 ) @@ -23,6 +24,7 @@ bottom_radius = 0.088 height = 0.429 [node name="GooGun" type="Spatial"] +script = ExtResource( 3 ) [node name="Body" type="MeshInstance" parent="."] mesh = SubResource( 1 ) |