Skip to content

Commit

Permalink
Tons of fixes + Combat3D sub-node to give combat attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
sophfee committed Apr 26, 2023
1 parent 2254972 commit 2ac0e6d
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 22 deletions.
1 change: 1 addition & 0 deletions Clock/GameTime.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extends Node
14 changes: 14 additions & 0 deletions Clock/TimeSince.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@icon("res://editor/icons/GameTime.svg")

# Simple class, place in the tree and access easily, contains a global float
# that holds the time elapsed since the scene has been booted up.
class_name GameTime
extends Node

var _curtime: float = 0;
func _process(delta):
_curtime += delta;

var elapsed_time: float:
get:
return _curtime;
36 changes: 36 additions & 0 deletions CombatNode3D/Combat3D.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright (c) 2023 - Nick S. - All Rights Reserved.
# ===================================================
# This file was shared publicly, please do not remove
# this credit part.
# https://github.com/urnotnick/godot-stuff
# ===================================================
class_name Combat3D
extends Node3D

@onready var _owner = get_parent_node_3d();
@export var starting_health: float = 100;
@export var maximum_health: float = 100;

# Private prop;
var _health: float = 100;
var health: float = 100:
get:
return (_health);

func _death(attacker: Combat3D) -> void:
pass

func _take_damage(attacker: Combat3D, damage: float, normal: Vector3, force: Vector3) -> void:
pass;

func _scale_damage(attacker: Combat3D, damage: float, normal: Vector3, force: Vector3) -> float:
return 1.0;

# This has to be called within _process_physics, otherwise you get bad results.
func inflict_damage(attacker: Combat3D, damage: float, normal: Vector3, force: Vector3, pos: Vector3) -> void:
get_parent_node_3d().apply_impulse(force);
damage = _scale_damage(attacker, damage, normal, force);
_health -= damage;
_take_damage(attacker, damage, normal, force);
if (_health <= 0):
_death(attacker);
Empty file added CombatNode3D/CombatNode3D.gd
Empty file.
33 changes: 29 additions & 4 deletions FPSWeapon/FPSWeapon.gd
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Copyright (c) 2023 - Nick S. - All Rights Reserved.
# ===================================================
# This file was shared publicly, please do not remove
# this credit part.
# https://github.com/urnotnick/godot-stuff
# ===================================================
class_name FPSWeapon
extends Node3D

Expand All @@ -14,13 +20,16 @@ var flash_time: float = 0;
@export var skew: float = 1.0;

@export_category("Stats")
@export var damage: float = 6.0;
@export var base_recoil: float = 0.46;
@export var rounds_per_minute: float = 450;
@export var automatic: bool = false;

@onready var view_model: ViewModel = get_parent();
@onready var fire_sounds: AudioStreamPlayer3D = find_child("FireSounds", true);
@onready var muzzle_flash_light: OmniLight3D = find_child("Flash", true);
@onready var muzzle_particle: MuzzleEffect = find_child("Particle", true);
@onready var combat_object: Combat3D = view_model.get_parent_node_3d().find_child("Combat3D");

var ironsights: bool = false:
get:
Expand All @@ -35,16 +44,16 @@ func primary_attack() -> void:
func view_punch(punch: Vector3) -> void:

# Ensure we have a camera.
assert(camera != null, "You do not have your camera identified.");
assert(camera, "You do not have your camera identified.");

camera.rotation += (camera.transform.basis * punch);

func play_anim(anim_name: String) -> void:
assert(animator != null, "You do not have a linked animator.");
assert(animator, "You do not have a linked animator.");
animator.play(anim_name);

func primary_attack_sound() -> void:
assert(fire_sounds != null, "You do not have fire sounds setup with your weapon.");
assert(fire_sounds);
fire_sounds.play(0.0);

var bullet_hole_scene: Node = preload("res://private-shared/cago/decals/BulletHole.tscn").instantiate();
Expand All @@ -62,6 +71,22 @@ func can_primary_attack() -> bool:
func fire_bullet(ray_caster: RayCast3D) -> void:
var hit: bool = ray_caster.get_collider() != null;

if (hit):
print("pee")
var obj := ray_caster.get_collider().find_child("Combat3D") as Combat3D;

if (obj == null):
return;
assert(combat_object);


obj.inflict_damage(
combat_object,
damage,
ray_caster.get_collision_normal(),
(ray_caster.to_global(Vector3.ZERO) - ray_caster.get_collision_point()).normalized() * -.8,
ray_caster.get_collision_point() - ray_caster.get_collider().position
);

func __fire_bullet(ray_caster: RayCast3D):
var hit: bool = ray_caster.get_collider() != null;
Expand All @@ -82,7 +107,7 @@ func __fire_bullet(ray_caster: RayCast3D):
func muzzle_flash() -> void:
flash_time = 0.05;
muzzle_particle.emit();
muzzle_flash_light.visible = true;
muzzle_flash_light.visible = false;

func _physics_process(delta):
if (flash_time == 0):
Expand Down
1 change: 0 additions & 1 deletion FirstPersonPlayer/FreeCamera.gd
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ var footstep_timer: float = 0
var footstep_lastfoot: bool = false;

func footstep(Leftfoot: bool, Velocity: float):
print("Footstep Velocity: ", Velocity);
if (Leftfoot):
footstep_stream.play();
else:
Expand Down
2 changes: 1 addition & 1 deletion FirstPersonPlayer/player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")

var target_motion: Vector3;

var lateral_velocity:float:
var lateral_velocity: float:
get:
return target_motion.x + target_motion.z

Expand Down
10 changes: 10 additions & 0 deletions NodeEXT/node_ext.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2023 - Nick S. - All Rights Reserved.
# ===================================================
# This file was shared publicly, please do not remove
# this credit part.
# https://github.com/urnotnick/godot-stuff
# ===================================================
extends Node;
func assert_instance(node_instance: Node) -> Node:
assert(node_instance != null, "Assertion of NULL instance.");
return node_instance;
25 changes: 9 additions & 16 deletions ViewModel/ViewModel.gd
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
class_name ViewModel
extends Node3D

var delta_x: float = 0
var delta_y: float = 0

@export_category("Viewmodel Bobbing")
@export var bob_right: float = 4;
@export var bob_right_rate: float = 8.4;
@export var bob_up: float = 2.3;
@export var bob_up_rate: float = 16.8;

@export_category("Viewmodel Sway")
@export var sway_x_multiplier: float = 0.06;
@export var sway_x_yaw_multiplier: float = -.28;
Expand All @@ -18,32 +14,30 @@ var delta_y: float = 0
@export var sway_y_multiplier: float = 0.06;
@export var sway_y_position_multiplier: float = -.02;
@export var sway_y_pitch_multiplier: float = .20;

@export_category("Viewmodel Modifiers")
@export var ironsights_sway_multiplier: float = .2;

var ironsights_alpha: float = 0

# We store this so we can apply an ever so slight lerp to prevent jitter
var mv: float = 0;

@onready var camera: Camera3D = $"../FirstPersonCamera";
@onready var pawn: CharacterBody3D = get_parent();

@onready var view_model: Node3D = $weapon_m4a4;

var curtime: float = 0;
var ironsights_alpha: float = 0
var mv: float = 0;
var xmv: float = 0;
var delta_x: float = 0
var delta_y: float = 0

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

func walk_bob(md: float) -> Vector3:
var a: float = md * lerpf(1, ironsights_sway_multiplier, ironsights_alpha);
var pos: Vector3 = Vector3(0, 0, 0);

pos.x += sin(curtime * bob_right_rate) * (a * bob_right);
pos.y -= cos(curtime * bob_up_rate) * (a * bob_up);
pos.x += sin(curtime * bob_right_rate) * (a * bob_right) + (a * 1.9);
pos.y -= cos(curtime * bob_up_rate) * (a * bob_up) + (a * 1.32);
pos.z += a * 6;

return pos;

var _punch_pos: Vector3 = Vector3(0, 0, 0);
Expand All @@ -58,7 +52,6 @@ func _input(event):
delta_x += deg_to_rad(event.relative.x * .06);
delta_y += deg_to_rad(event.relative.y * .06);

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float):
curtime += delta;

Expand Down

0 comments on commit 2ac0e6d

Please sign in to comment.