broken_seals/game/player/Mob.gd

359 lines
9.2 KiB
GDScript3
Raw Normal View History

2019-11-20 14:05:35 +01:00
extends Entity
# Copyright Péter Magyar relintai@gmail.com
# MIT License, functionality from this class needs to be protable to the entity spell system
2019-11-20 14:05:35 +01:00
2020-01-31 20:01:34 +01:00
# Copyright (c) 2019-2020 Péter Magyar
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
2019-11-20 14:05:35 +01:00
#export (String) var map_path : String
export(float) var max_visible_distance : float = 120 setget set_max_visible_distance
var max_visible_distance_squared : float = max_visible_distance * max_visible_distance
const ray_length = 1000
const ACCEL : float = 100.0
const DEACCEL : float = 100.0
const GRAVITY : float = -24.8
const JUMP_SPEED : float = 3.8
const MAX_SLOPE_ANGLE : float = 40.0
var _on : bool = true
var y_rot : float = 0.0
var vel : Vector3 = Vector3()
var dir : Vector3 = Vector3()
var target_movement_direction : Vector2 = Vector2()
var animation_tree : AnimationTree
var anim_node_state_machine : AnimationNodeStateMachinePlayback = null
var animation_run : bool = false
var moving : bool = false
var sleep : bool = false
var dead : bool = false
var death_timer : float = 0
var _world : VoxelWorld = null
2019-11-20 14:05:35 +01:00
func _ready() -> void:
animation_tree = get_character_skeleton().get_animation_tree()
if animation_tree != null:
anim_node_state_machine = animation_tree["parameters/playback"]
animation_tree["parameters/run-loop/blend_position"] = Vector2(0, -1)
ai_state = EntityEnums.AI_STATE_PATROL
func _enter_tree():
_world = get_node("..") as VoxelWorld
set_process(true)
set_physics_process(true)
2019-11-20 14:05:35 +01:00
func _process(delta : float) -> void:
if dead:
death_timer += delta
if death_timer > 60:
queue_free()
return
2019-11-20 14:05:35 +01:00
var camera : Camera = get_tree().get_root().get_camera() as Camera
if camera == null:
return
var cam_pos : Vector3 = camera.global_transform.xform(Vector3())
2020-01-02 23:02:45 +01:00
var dstv : Vector3 = cam_pos - get_body().translation
2019-11-20 14:05:35 +01:00
dstv.y = 0
var dst : float = dstv.length_squared()
if dst > max_visible_distance_squared:
2020-01-02 23:02:45 +01:00
if get_body().visible:
get_body().hide()
2019-11-20 14:05:35 +01:00
return
else:
# var lod_level : int = int(dst / max_visible_distance_squared * 3.0)
if dst < 400: #20^2
get_character_skeleton().set_lod_level(0)
elif dst > 400 and dst < 900: #20^2, 30^2
get_character_skeleton().set_lod_level(1)
else:
get_character_skeleton().set_lod_level(2)
2020-01-02 23:02:45 +01:00
if not get_body().visible:
get_body().show()
2019-11-20 14:05:35 +01:00
func _physics_process(delta : float) -> void:
if not _on:
return
if sentity_data == null:
return
if dead:
return
if _world != null:
if not _world.is_position_walkable(get_body().transform.origin):
return
2019-11-20 14:05:35 +01:00
process_movement(delta)
func process_movement(delta : float) -> void:
if starget != null:
2020-01-02 23:02:45 +01:00
get_body().look_at(starget.get_body().translation, Vector3(0, 1, 0))
2019-11-20 14:05:35 +01:00
var state : int = getc_state()
if state & EntityEnums.ENTITY_STATE_TYPE_FLAG_ROOT != 0 or state & EntityEnums.ENTITY_STATE_TYPE_FLAG_STUN != 0:
moving = false
return
if target_movement_direction.length_squared() > 0.1:
if anim_node_state_machine != null and not animation_run:
anim_node_state_machine.travel("run-loop")
animation_run = true
target_movement_direction = target_movement_direction.normalized()
moving = true
else:
if anim_node_state_machine != null and animation_run:
anim_node_state_machine.travel("idle-loop")
animation_run = false
moving = false
if target_movement_direction.x > 0.1 or target_movement_direction.y > 0.1 or target_movement_direction.x < -0.1 or target_movement_direction.y < -0.1:
y_rot = Vector2(0, 1).angle_to(target_movement_direction)
var forward : Vector3 = Vector3(0, 0, 1).rotated(Vector3(0, 1, 0), deg2rad(y_rot))
var right : Vector3 = forward.cross(Vector3(0, 1, 0)) * -target_movement_direction.x
forward *= target_movement_direction.y #only potentially make it zero after getting the right vector
dir = forward
dir += right
if dir.length_squared() > 0.1:
dir = dir.normalized()
moving = true
else:
dir = Vector3()
moving = false
if not moving and sleep:
return
if moving and sleep:
sleep = false
vel.y += delta * GRAVITY
var hvel : Vector3 = vel
hvel.y = 0
var target : Vector3 = dir
target *= get_speed().ccurrent
var accel
if dir.dot(hvel) > 0:
accel = ACCEL
else:
accel = DEACCEL
hvel = hvel.linear_interpolate(target, accel * delta) as Vector3
vel.x = hvel.x
vel.z = hvel.z
var facing : Vector3 = vel
facing.y = 0
2020-01-02 23:02:45 +01:00
vel = get_body().move_and_slide(vel, Vector3(0,1,0), false, 4, deg2rad(MAX_SLOPE_ANGLE))
sset_position(get_body().translation, get_body().rotation)
2019-11-20 14:05:35 +01:00
if vel.length_squared() < 0.12:
sleep = true
2020-01-02 23:02:45 +01:00
if get_body().translation.y < -50.0:
2019-11-20 14:05:35 +01:00
print("killed mob with fall damage")
var sdi : SpellDamageInfo = SpellDamageInfo.new()
sdi.damage_source_type = SpellDamageInfo.DAMAGE_SOURCE_UNKNOWN
sdi.damage = 999999999
stake_damage(sdi)
func rotate_delta(x_delta : float) -> void:
y_rot += x_delta
if y_rot > 360:
y_rot = 0
if y_rot < 0:
y_rot = 360
2020-01-02 23:02:45 +01:00
get_body().rotation_degrees = Vector3(0.0, y_rot, 0.0)
2019-11-20 14:05:35 +01:00
func sstart_attack(entity : Entity) -> void:
ai_state = EntityEnums.AI_STATE_ATTACK
starget = entity
func _onc_mouse_enter() -> void:
if centity_interaction_type == EntityEnums.ENITIY_INTERACTION_TYPE_LOOT:
Input.set_default_cursor_shape(Input.CURSOR_CROSS)
else:
Input.set_default_cursor_shape(Input.CURSOR_MOVE)
func _onc_mouse_exit() -> void:
Input.set_default_cursor_shape(Input.CURSOR_ARROW)
func _son_death():
if dead:
return
if starget == null:
queue_free()
return
#warning-ignore:unused_variable
for i in range(gets_aura_count()):
removes_aura(gets_aura(0))
2019-11-20 14:05:35 +01:00
dead = true
var ldiff : float = scharacter_level - starget.scharacter_level + 10.0
2019-11-20 14:05:35 +01:00
if ldiff < 0:
ldiff = 0
if ldiff > 15:
ldiff = 15
ldiff /= 10.0
starget.adds_xp(int(5.0 * scharacter_level * ldiff))
2019-11-20 14:05:35 +01:00
starget = null
sentity_interaction_type = EntityEnums.ENITIY_INTERACTION_TYPE_LOOT
ai_state = EntityEnums.AI_STATE_OFF
anim_node_state_machine.travel("dead")
# set_process(false)
set_physics_process(false)
remote func set_position(position : Vector3, rotation : Vector3) -> void:
if get_tree().is_network_server():
rpc("set_position", position, rotation)
func _son_damage_dealt(data):
if ai_state != EntityEnums.AI_STATE_ATTACK and data.dealer != self:
sstart_attack(data.dealer)
func _con_damage_dealt(info : SpellDamageInfo) -> void:
# if info.dealer ==
2020-01-02 23:02:45 +01:00
WorldNumbers.damage(get_body().translation, 1.6, info.damage, info.crit)
2019-11-20 14:05:35 +01:00
func _con_heal_dealt(info : SpellHealInfo) -> void:
2020-01-02 23:02:45 +01:00
WorldNumbers.heal(get_body().translation, 1.6, info.heal, info.crit)
2019-11-20 14:05:35 +01:00
func _moved() -> void:
if sis_casting():
sfail_cast()
func set_max_visible_distance(var value : float) -> void:
max_visible_distance_squared = value * value
max_visible_distance = value
#func _setup():
# sentity_name = sentity_data.text_name
2019-11-20 14:05:35 +01:00
func _son_xp_gained(value : int) -> void:
if not ESS.get_resource_db().get_xp_data().can_character_level_up(gets_character_level()):
2019-11-20 14:05:35 +01:00
return
var xpr : int = ESS.get_resource_db().get_xp_data().get_character_xp(gets_character_level());
2019-11-20 14:05:35 +01:00
if xpr <= scharacter_xp:
scharacter_levelup(1)
scharacter_xp = 0
2019-11-20 14:05:35 +01:00
func _son_class_level_up(value: int):
._son_class_level_up(value)
refresh_spells(value)
func _son_character_level_up(value: int) -> void:
._son_character_level_up(value)
refresh_spells(value)
2019-11-20 14:05:35 +01:00
func refresh_spells(value: int):
if gets_free_spell_points() == 0 and gets_free_talent_points() == 0:
return
2019-11-20 14:05:35 +01:00
var ecd : EntityClassData = sentity_data.entity_class_data
if ecd == null:
return
var arr : Array = Array()
for i in range(ecd.get_num_spells()):
arr.append(ecd.get_spell(i))
randomize()
arr.shuffle()
2019-12-24 10:22:56 +01:00
for _v in range(value):
2019-11-20 14:05:35 +01:00
for i in range(arr.size()):
var spell : Spell = arr[i]
if not hass_spell(spell):
var spnum :int = gets_spell_count()
crequest_spell_learn(spell.id)
if spnum != gets_spell_count():
break
if sfree_spell_points == 0:
break
if sfree_spell_points == 0:
break
func sset_position(position : Vector3, rotation : Vector3) -> void:
if multiplayer.network_peer and multiplayer.is_network_server():
# cset_position(position, rotation)
2020-01-02 23:02:45 +01:00
vrpc("cset_position", position, rotation)
2019-11-20 14:05:35 +01:00
remote func cset_position(position : Vector3, rotation : Vector3) -> void:
2020-01-02 23:02:45 +01:00
get_body().translation = position
get_body().rotation = rotation
2019-11-20 14:05:35 +01:00