Big update

This commit is contained in:
2026-03-07 03:14:10 +03:00
parent f8f69d3b88
commit a998771486
33 changed files with 1788 additions and 577 deletions

View File

@@ -1,3 +1,15 @@
local PLAYER_PHYS_MASS = 80.0
local PLAYER_PHYS_RADIUS = 0.40
local PLAYER_PHYS_HEIGHT = 1.79
local PLAYER_PHYS_JUMPDIST = PLAYER_PHYS_RADIUS
local PLAYER_PHYS_JUMPHEIGHT = 2.0
local PLAYER_PHYS_JUMPSPEEDY = 5.0
local PLAYER_PHYS_WALK_SPEED = 5.5
local PLAYER_PHYS_RUN_SPEED_MUL = 1.4
local PLAYER_PHYS_MOVE_SPEED_EXP = 1.0
local PLAYER_PHYS_FLY_SPEED_EXP = 4.0
local AIR_CONTROL = 0.2
-- игрок
actor_player = inherit_table(actor_base)
@@ -6,61 +18,162 @@ actor_player.m_camera_offset_y = 0.5
function actor_player:on_init()
actor_base.on_init(self)
self:create_body()
g_player = self
self:create_player_body(PLAYER_PHYS_RADIUS, PLAYER_PHYS_HEIGHT - PLAYER_PHYS_RADIUS * 2.0, 80.0, 0.0, 0.0)
self:activate_camera()
local ent = engine.create_entity("test_object")
local ent = engine.create_entity("weapon_ump")
engine.add_entity_to_world(ent)
self.m_weapon_entity_id = ent:get_id()
self.m_in_reload = false
--local ent2 = engine.get_entity_from_id(self.m_weapon_entity_id)
--console.print(ent2:get_classname())
end
function actor_player:on_shutdown()
actor_base.on_shutdown(self)
g_player = nil
end
function actor_player:on_update(dt)
actor_base.on_update(self, dt)
self:update_camera_look()
--self:update_camera_movement(dt)
self:update_body_movement(dt)
self:update_camera_look() -- C++
--self:update_camera() Lua (for perfomance should call from C++)
--self:update_camera_movement(dt) -- C++ flying mode
--self:update_body_movement(dt) -- C++
self:update_player_movement(dt)
local ent = engine.get_entity_from_id(self.m_weapon_entity_id)
ent:set_relative_position_to_camera(self)
if self:get_action() == ACTION_FIRE and self.m_in_reload == false then
ent:play_animation(ent:find_animation("shoot1"), ANIM_PLAYBACK_NONE)
engine.play_sound("data/sounds/weapons/ump45_shoot.wav")
self:action_update()
end
function actor_player:action_update()
local action = self:get_action()
local ent = engine.get_entity_from_id(self.m_weapon_entity_id)
if ent then
if action == ACTION_FIRE then
ent:set_state(WEAPON_FSM_STATE_ATTACK)
elseif action == ACTION_RELOAD then
ent:set_state(WEAPON_FSM_STATE_RELOAD)
end
end
end
-- TODO: remove
function vec3_magnitude(_x, _y, _z)
return math.sqrt(_x * _x + _y * _y + _z * _z)
end
function vec3_normalize(_x, _y, _z)
local mag = vec3_magnitude(_x, _y, _z)
if mag == 0 then return 0, 0, 0 end
return _x / mag, _y / mag, _z / mag
end
function vec3_cross(_x1, _y1, _z1, _x2, _y2, _z2)
return _y1 * _z2 - _y2 * _z1,
_z1 * _x2 - _z2 * _x1,
_x1 * _y2 - _x2 * _y1
end
function actor_player:update_player_movement(dt)
local movement = self:get_movement()
local speed = 4.0
local up_x, up_y, up_z = camera.get_up()
local front_x, front_y, front_z = camera.get_front()
front_y = 0.0
local final_front_x, final_front_y, final_front_z = vec3_normalize(front_x, front_y, front_z)
local cross_x, cross_y, cross_z = vec3_cross(final_front_x, final_front_y, final_front_z,
up_x, up_y, up_z)
local final_cross_x, final_cross_y, final_cross_z = vec3_normalize(cross_x, cross_y, cross_z)
local dir_x, dir_y, dir_z = 0.0, 0.0, 0.0
if (movement & EMovementDir_Forward) ~= 0 then
dir_x = dir_x + final_front_x
dir_y = dir_y + final_front_y
dir_z = dir_z + final_front_z
end
if (movement & EMovementDir_Backward) ~= 0 then
dir_x = dir_x - final_front_x
dir_y = dir_y - final_front_y
dir_z = dir_z - final_front_z
end
if (movement & EMovementDir_Left) ~= 0 then
dir_x = dir_x - final_cross_x
dir_y = dir_y - final_cross_y
dir_z = dir_z - final_cross_z
end
if (movement & EMovementDir_Right) ~= 0 then
dir_x = dir_x + final_cross_x
dir_y = dir_y + final_cross_y
dir_z = dir_z + final_cross_z
end
local current_vel_x, current_vel_y, current_vel_z = self:get_velocity()
local vel_x = dir_x * speed
local vel_y = dir_y * speed
local vel_z = dir_z * speed
if self:on_ground() then
self:set_velocity(vel_x, current_vel_y, vel_z)
if (movement & EMovementDir_Jump) ~= 0 then
self:set_velocity(current_vel_x, PLAYER_PHYS_JUMPSPEEDY, current_vel_z)
--console.print("!!! JUMP")
end
else
local air_vel_x = current_vel_x + dir_x * speed * AIR_CONTROL * dt
local air_vel_y = current_vel_y + dir_y * speed * AIR_CONTROL * dt
local air_vel_z = current_vel_z + dir_z * speed * AIR_CONTROL * dt
self:set_velocity(air_vel_x, current_vel_y, air_vel_z)
end
end
local g_yaw = 0.0
local g_pitch = 0.0
function actor_player:update_camera()
if not input.get_lock_mouse() then
return
end
if self:get_action() == ACTION_RELOAD and self.m_in_reload == false then
ent:play_animation(ent:find_animation("reload"), ANIM_PLAYBACK_NONE)
engine.play_sound("data/sounds/weapons/ump45_reload.wav")
self.m_in_reload = true
end
if ent:get_current_animation() == ent:find_animation("shoot1") and
ent:get_current_animation_time() >= ent:get_animation_time(ent:find_animation("shoot1")) then
ent:play_animation(ent:find_animation("idle1"), ANIM_PLAYBACK_REPEAT)
end
if self.m_in_reload == true and ent:get_current_animation_time() >= ent:get_animation_time(ent:find_animation("reload")) then
ent:play_animation(ent:find_animation("idle1"), ANIM_PLAYBACK_REPEAT)
self.m_in_reload = false
end
-- if ent:get_current_animation() == ent:find_animation("reload") and
-- ent:get_current_animation_time() >= ent:get_animation_time(ent:find_animation("reload")) then
-- ent:play_animation(ent:find_animation("idle1"), ANIM_PLAYBACK_NONE)
--end
local mousePosX, mousePosY = input.get_mouse_pos()
local sensitivity = 0.15 -- #TODO: input.get_mouse_sensitivity()
g_yaw = g_yaw + (mousePosX * sensitivity)
g_pitch = g_pitch - (mousePosY * sensitivity)
-- lock axis
if g_pitch > 89.0 then g_pitch = 89.0 end
if g_pitch < -89.0 then g_pitch = -89.0 end
camera.set_yaw_pitch(g_yaw, g_pitch)
end
function actor_player:on_collide(other)
console.print(string.format("actor_player:on_collide: %s", other:get_classname()))
--console.print(string.format("actor_player:on_collide: %s", other:get_classname()))
-- put_debug_string_to_screen(string.format("actor_player:on_collide: %s", other:get_classname()), 4)
-- FOR TEST
-- other:mark_for_delete()
end
function actor_player:get_health()
return 100.0
end

70
data/scripts/game_hud.lua Normal file
View File

@@ -0,0 +1,70 @@
local RED_COLOR = { 1.0, 0.0, 0.0, 1.0 }
local GREEN_COLOR = { 0.0, 1.0, 0.0, 1.0 }
local BLUE_COLOR = { 0.0, 0.0, 1.0, 1.0 }
local BLACK_COLOR = { 0.0, 0.0, 0.0, 1.0 }
local WHITE_COLOR = { 1.0, 1.0, 1.0, 1.0 }
local draw_test_hud = true
local draw_debug_string = false
local debug_string_text = ""
local debug_string_time = 0.0
local debug_string_max_time = 0.0
function game_hud_draw( )
if not draw_test_hud then
return
end
-- example of color
local color = { 0.5, 0.5, 0.1, 1.0 }
-- example of drawing
ui.draw_rect(150.0, 150.0, 350.0, 350.0, BLUE_COLOR)
ui.draw_image("data/textures/koshka1.jpg", 160.0, 160.0, 340.0, 340.0, WHITE_COLOR)
ui.draw_text("Hello, world!", 200.0, 200.0, GREEN_COLOR)
if g_player then
game_player_hud_draw()
end
-- debug
if draw_debug_string then
game_debug_string_draw()
end
end
function game_player_hud_draw()
local display_x, display_y = ui.get_display_size()
local color = { 0.0, 0.0, 0.0, 0.3 }
local text_color = { 0.0, 0.0, 0.0, 0.6 }
local hud_rect_size_x = 200
local hud_rect_size_y = 100
local offset_text = ui.calc_text_width("Health") + 5
ui.draw_rect(0.0, display_y, hud_rect_size_x, display_y - hud_rect_size_y, color)
ui.draw_text("Health", 10.0, display_y - hud_rect_size_y + 20, WHITE_COLOR)
ui.draw_text(string.format("%.0f", g_player:get_health()), 10.0 + offset_text, display_y - hud_rect_size_y + 20, WHITE_COLOR)
end
function put_debug_string_to_screen(text, text_time)
debug_string_max_time = text_time
debug_string_text = text
debug_string_time = 0.0
draw_debug_string = true
end
function game_debug_string_draw()
debug_string_time = debug_string_time + engine.get_delta()
if debug_string_time >= debug_string_max_time then
debug_string_max_time = 0.0
debug_string_time = 0.0
draw_debug_string = false
end
local offset = ui.calc_text_width(debug_string_text)
ui.draw_rect(490, 500, 490 + offset + 20, 550, BLACK_COLOR)
ui.draw_text(debug_string_text, 500, 500, RED_COLOR)
end

View File

@@ -1,25 +1,39 @@
-- Game initialization script
console.print("--- Game initialization ---")
-- загружаем скрипты
load_script("game_utils.lua")
load_script("game_hud.lua")
load_script("game_object.lua")
load_script("test_object.lua")
load_script("weapons/weapon_base.lua")
load_script("weapons/weapon_ump.lua")
load_script("actors/actor_base.lua")
load_script("actors/actor_player.lua")
-- глобальные переменные
g_player = nil
-- глобальная таблица сущностей
g_entity_table = {
-- Lua class -- CPP class -- Description
-- Actors
{ "actor_player", "ActorBase", "Player entity" },
-- Weapons
{ "weapon_ump", "WeaponBase", "Weapon UMP" },
-- Simple entity
{ "test_object", "Entity", "Test entity" },
}
}
function sv_game_init( )
console.print("--- Game initialization ---")
end

View File

@@ -1,65 +0,0 @@
Globals:
ACTION_FIRE = 0
ACTION_ALT_FIRE = 1
ACTION_RELOAD = 2
ANIM_PLAYBACK_NONE = 0
ANIM_PLAYBACK_REPEAT = 1
engine.error(string message)
engine.warning(string message)
engine.create_entity(string classname) -- return an table to the new entity
engine.add_entity_to_world(entity) -- add entity to the world
engine.get_entity_from_id(integer id) -- return an entity table from
engine.play_sound(string filename) -- play 2d sound
camera.get_position() -- return x, y, z
camera.get_front() -- return x, y, z
camera.get_right() -- return x, y, z
camera.get_up() -- return float
camera.get_pitch() -- return float
console.print(string message)
Entity properties:
string m_name
integer m_id
Entity methods:
load_model(string filename)
update_transform()
translate(float x, float y, float z) -- addition to the current position
set_position(float x, float y, float z) -- setting the position
get_position() -- return x, y, z
set_rotation(float x, float y, float z) -- setting the euler rotation
set_rotation_from_vectors(float frontx, float fronty, float frontz,
float rightx, float righty, float rightz,
float upx, float upy, float upz) -- rotate around axis
get_classname() -- return the classname of the entity
get_id() -- return the id of the entity
find_animation(string name) -- find a animation in the model
play_animation(integer id, integer mode) -- play a animation with specified mode (ANIM_PLAYBACK_NONE, ANIM_PLAYBACK_REPEAT)
get_current_animation() -- return the id to the current playing animation
get_current_animation_time() -- return the current time of the current playing animation
get_animation_time(integer id) -- return the time of a animation
ActorBase methods:
activate_camera()
update_camera_look()
update_camera_movement(float nubmer)
create_body()
update_body_movement(float nubmer)
get_action() -- return an current mode (ACTION_FIRE, ACTION_ALT_FIRE, ACTION_RELOAD)

View File

@@ -1,11 +1,12 @@
-----------------------------------------------------------
-- weapon_base.lua, Базоавый скрипт оружия
-- weapon_base.lua, Базовый скрипт оружия
-- Автор: Кирилл
-- Изменяли:
-- Дата: 05.03.2026
-----------------------------------------------------------
-- индификаторы состояний FSM
WEAPON_FSM_STATE_NONE = 0
WEAPON_FSM_STATE_IDLE = 1
WEAPON_FSM_STATE_ATTACK = 2
WEAPON_FSM_STATE_ATTACK2 = 3
@@ -17,32 +18,11 @@ weapon_base = inherit_table(game_object)
-- инициализация FSM
weapon_base.m_fsm = {}
-- покой
weapon_base.m_fsm[WEAPON_FSM_STATE_IDLE] = {
anim = "idle", -- имя анимации
anim_playback = ANIM_PLAYBACK_REPEAT, -- бесконечно играть
anim_speed = 1.0 -- обычная скорость анимации
}
-- атака
weapon_base.m_fsm[WEAPON_FSM_STATE_ATTACK] = {
anim = "attack", -- имя анимации
anim_playback = ANIM_PLAYBACK_NONE,
anim_speed = 1.0 -- обычная скорость анимации
}
-- перезарядка
weapon_base.m_fsm[WEAPON_FSM_STATE_RELOAD] = {
anim = "reload", -- имя анимации
anim_playback = ANIM_PLAYBACK_NONE,
anim_speed = 1.0 -- обычная скорость анимации
}
function weapon_base:on_init()
game_object.on_init(self)
-- начальное состояние FSM
self.m_state = WEAPON_FSM_STATE_IDLE
self.m_state = WEAPON_FSM_STATE_NONE
self.m_next_state = WEAPON_FSM_STATE_IDLE
self.m_state_time = 0.0
self.m_end_state_time = 0.0
@@ -64,46 +44,96 @@ function weapon_base:on_fsm_state_update(dt)
self.m_state_time = self.m_state_time + dt
-- проверка на стрельбу
if (self.m_state == WEAPON_STATE_ATTACK or
self.m_state == WEAPON_STATE_ATTACK2) and
if (self.m_state == WEAPON_FSM_STATE_ATTACK or
self.m_state == WEAPON_FSM_STATE_ATTACK2) and
self.m_state_time >= self.m_end_state_time then
-- переходим в ожидание
self:set_state(WEAPON_STATE_IDLE)
self:set_state(WEAPON_FSM_STATE_IDLE)
end
-- костыль, нету анимаций бесконечных
if self.m_state == WEAPON_STATE_IDLE and self.m_state_time >= self.m_end_state_time then
if self.m_state == WEAPON_FSM_STATE_IDLE and self.m_state_time >= self.m_end_state_time then
-- переходим в ожидание
self:set_state(WEAPON_STATE_IDLE)
self:set_state(WEAPON_FSM_STATE_IDLE)
end
self:fsm_update(dt)
end
function weapon_base:set_state(next_state)
if self.m_state == next_state or self.m_next_state == next_state then
return
end
if (self.m_state == WEAPON_FSM_STATE_ATTACK or self.m_state == WEAPON_FSM_STATE_RELOAD)
and next_state ~= WEAPON_FSM_STATE_IDLE then
return
end
self.m_next_state = next_state
console.print(string.format("weapon_base:set_state: switich to '%d' ", next_state))
end
function weapon_base:fsm_update(dt)
-- надо ли менять состояние
if self.m_next_state ~= self.m_state then
if self.m_next_state == WEAPON_STATE_ATTACK then
self:set_anim("attack1")
elseif self.m_next_state == WEAPON_STATE_ATTACK2 then
self:set_anim("attack1")
end
self:on_state_switch(self.m_next_state)
-- сброс времени и установка его на таймер анимации
self.m_state_time = 0.0
self.m_end_state_time = render.get_anim_time(self:get_anim())
self.m_end_state_time = self:get_animation_time(self:get_current_animation())
-- запускаем атаку
if self.m_next_state == WEAPON_STATE_ATTACK or self.m_next_state == WEAPON_STATE_ATTACK2 then
self:attack()
end
console.print(string.format("weapon_base:fsm_update: switched from '%d' to '%d', end time %f ",
self.m_state, self.m_next_state, self.m_end_state_time))
self.m_state = self.m_next_state
end
end
end
function weapon_base:find_fsm_state(state)
for k, v in pairs(self.m_fsm) do
if k == state then
return v
end
end
console.print(string.format("weapon_base:find_fsm_state: state '%d' not found or not registered", state))
return nil
end
function weapon_base:on_state_switch(state)
-- ищем стейт в таблице
local fsm_state = self:find_fsm_state(state)
if not fsm_state then
return
end
local anim_id = self:find_animation(fsm_state.anim)
self:play_animation(anim_id, fsm_state.anim_playback)
end
function weapon_base:set_relative_position_to_camera( ent )
local camX, camY, camZ = camera.get_position()
local frontX, frontY, frontZ = camera.get_front()
local rightX, rightY, rightZ = camera.get_right()
local upX, upY, upZ = camera.get_up()
self:set_rotation_from_vectors(frontX, frontY, frontZ,
rightX, rightY, rightZ,
upX, upY, upZ)
local offsetx = 0.0
local offsety = 0.0
local offsetz = 0.0
local x = camX + (rightX * offsetx) + (upX * offsety) + (frontX * offsetz)
local y = camY + (rightY * offsetx) + (upY * offsety) + (frontY * offsetz)
local z = camZ + (rightZ * offsetx) + (upZ * offsety) + (frontZ * offsetz)
self:set_position(x, y, z)
-- force update transform
self:update_transform()
end

View File

@@ -1 +0,0 @@
weapon_key = inherit_table(weapon_base)

View File

@@ -0,0 +1,33 @@
-----------------------------------------------------------
-- weapon_base.lua, Базоавый скрипт оружия
-- Автор: Кирилл
-- Изменяли:
-- Дата: 06.03.2026
-----------------------------------------------------------
weapon_ump = inherit_table(weapon_base)
-- покой
weapon_ump.m_fsm[WEAPON_FSM_STATE_IDLE] = {
anim = "idle1", -- имя анимации
anim_playback = ANIM_PLAYBACK_REPEAT, -- бесконечно играть
anim_speed = 1.0 -- обычная скорость анимации
}
-- атака
weapon_ump.m_fsm[WEAPON_FSM_STATE_ATTACK] = {
anim = "shoot1", -- имя анимации
anim_playback = ANIM_PLAYBACK_NONE,
anim_speed = 1.0 -- обычная скорость анимации
}
-- перезарядка
weapon_ump.m_fsm[WEAPON_FSM_STATE_RELOAD] = {
anim = "reload", -- имя анимации
anim_playback = ANIM_PLAYBACK_NONE,
anim_speed = 1.0 -- обычная скорость анимации
}
function weapon_ump:on_init()
weapon_base.on_init(self)
self:load_model("data/models/weapons/v_ump.iqm")
end

BIN
data/textures/koshka1.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 114 KiB