Skip to content

Commit

Permalink
Complete chapter 11.7
Browse files Browse the repository at this point in the history
  • Loading branch information
DaikonNimono committed Mar 15, 2024
1 parent 3f40e9a commit 7de38b7
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 13 deletions.
32 changes: 28 additions & 4 deletions data/FirstPersonShooter.world
Original file line number Diff line number Diff line change
Expand Up @@ -2557,7 +2557,9 @@
<mesh_name>guid://89640b0d7ffa9f7f02a1a97753abc138ef6e3f24</mesh_name>
<surface name="box" intersection="1" shadow_mask="0x0" viewport_mask="0x0" intersection_mask="0x2" material="37b5258e98b7907a6fdfa8cc5256713c8ffef517"/>
<properties>
<property parent="51e877b3c29f323185bb02f9fb15bbc3f3022b4b" guid="32fbb6e3f511f228f0ff32a98500d9171aac507b"/>
<property parent="51e877b3c29f323185bb02f9fb15bbc3f3022b4b" guid="32fbb6e3f511f228f0ff32a98500d9171aac507b">
<parameter name="health">30</parameter>
</property>
</properties>
<transform>1 -7.1054274e-15 2.9611872e-16 0.0 7.1054274e-15 1 2.4335929e-08 0.0 -2.9611893e-16 -2.4335929e-08 1 0.0 5.5368699e-21 3.765877e-14 0 1.0</transform>
</node>
Expand All @@ -2577,9 +2579,31 @@
<height>3</height>
<transform>1 0 0 0.0 0 1 0 0.0 0 0 1 0.0 0 0 0 1.0</transform>
</node>
<node type="NodeReference" id="1735244756" name="robot_enemy">
<reference>guid://1052eb5f16b3c132e06baee99e58ab34533a5d03</reference>
<transform>1 0 0 0.0 0 1 0 0.0 0 0 1 0.0 0.075533859 5.9413724 1.5 1.0</transform>
<node type="NodeDummy" id="403641838" name="gameplay_systems">
<properties>
<property parent="aa9f1fea1e56f7a6da31878c2789e44171c877b4" guid="35c9434ff1930d4f797ae235ce4e1dca29220167">
<parameter name="state">0</parameter>
<parameter name="EndCamera">472769653</parameter>
<parameter name="SpawnPoint">577391737</parameter>
<parameter name="enemyPrefab">guid://1052eb5f16b3c132e06baee99e58ab34533a5d03</parameter>
<parameter name="NumEnemies">3</parameter>
</property>
</properties>
<transform>1 0 0 0.0 0 1 0 0.0 0 0 1 0.0 0 0 0 1.0</transform>
</node>
<node type="NodeDummy" id="577391737" name="spawn_point">
<transform>1 0 0 0.0 0 1 0 0.0 0 0 1 0.0 -5.45014 -1.9063964 0.99999994 1.0</transform>
</node>
<node type="PlayerDummy" id="472769653" name="end_camera">
<fov>75</fov>
<znear>0.300000012</znear>
<zfar>100000</zfar>
<oblique_frustum_plane>0 0 -1 0</oblique_frustum_plane>
<is_oblique_frustum>0</is_oblique_frustum>
<is_main>0</is_main>
<is_listener>1</is_listener>
<controlled>1</controlled>
<transform>-0.8171438 -0.57643408 4.1443864e-06 0.0 0.038207922 -0.054155827 0.99780136 0.0 -0.57516634 0.81534731 0.066277385 0.0 -4.7989421 17.581108 4.3357677 1.0</transform>
</node>
</editor>
<console>
Expand Down
10 changes: 10 additions & 0 deletions data/csharp_template/FP_controller/components/Bullet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ private void Update()
hitEffect.WorldPosition = hitInfo.Point;
hitEffect.SetWorldDirection(hitInfo.Normal, vec3.UP, MathLib.AXIS.Y);

// проверяем объект, в который попали (hitObject), игрок ли это и есть ли у него компонента Health
Health health = hitObject.GetComponent<Health>();
if (health && hitObject.GetComponentInParent<PlayerLogic>())
{
// применяем ущерб от пули
health.TakeDamage(damage);

// обновляем информацию о здоровье игрока в HUD
ComponentSystem.FindComponentInWorld<HUD>().UpdateHealthInfo(health.health);
}
// удаляем пулю
node.DeleteLater();
}
Expand Down
19 changes: 14 additions & 5 deletions data/csharp_template/FP_controller/components/EnemyLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class EnemyLogic : Component

private Health health = null;

private GameController gameController = null;

private bool targetIsVisible;
private Vec3 lastSeenPosition;
private vec3 lastSeenDirection;
Expand Down Expand Up @@ -83,6 +85,9 @@ private void Init()
// берем компонент EnemyFireController
fireController = node.GetComponent<EnemyFireController>();

// находим компонент GameController
gameController = ComponentSystem.FindComponentInWorld<GameController>();

health = node.GetComponentInChildren<Health>();

shouldUpdateRoute = true;
Expand All @@ -92,6 +97,15 @@ private void Init()

private void Update()
{
// Проверяем текущее состояние, если игровой процесс остановлен, то враг не выполняет никаких действий
if (gameController.state != GameState.Gameplay)
return;

// проверяем, выставлен ли флаг IsDead
if (health && health.IsDead)
// удаляем врага со сцены, если он убит (IsDead)
node.DeleteLater();

UpdateTargetState();
UpdateOrientation();
UpdateRoute();
Expand All @@ -113,11 +127,6 @@ private void Update()
case EnemyLogicState.Attack: color = vec4.RED; break;
}

// проверяем, выставлен ли флаг IsDead
if (health && health.IsDead)
// удаляем врага со сцены, если он убит (IsDead)
node.DeleteLater();

// визуализируем состояния врага
Visualizer.RenderPoint3D(node.WorldPosition + vec3.UP * 2.0f, 0.25f, color);
Visualizer.RenderPoint3D(node.WorldPosition + vec3.UP * 3.0f, 0.25f, IsTargetVisible() ? vec4.GREEN : vec4.RED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<asset version="2.18.0.1">
<guid>5158593f996ef01aae4f3a2a8ce7a21d539470f6</guid>
<type>component</type>
<hash>0bbb0373</hash>
<hash>a4eb51e5</hash>
<runtimes>
<runtime id="5158593f996ef01aae4f3a2a8ce7a21d539470f6" name="EnemyLogic.cs" link="0"/>
<runtime id="5efec285bbcacbfd75d58238dff8fedd1ee54024" name="EnemyLogic.prop" link="1" type="4"/>
Expand Down
67 changes: 67 additions & 0 deletions data/csharp_template/FP_controller/components/GameController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Unigine;

public enum GameState
{
Gameplay,
Win,
Lose,
}

[Component(PropertyGuid = "aa9f1fea1e56f7a6da31878c2789e44171c877b4")]
public class GameController : Component
{
public GameState state;
public Player EndCamera = null; // Камера для финала игры

public NodeDummy SpawnPoint = null;

[ParameterFile]
public AssetLink enemyPrefab = null;
public int NumEnemies = 10;

private int spawned_enemy_counter = 0;
public float spawnInterval = 2.0f;
private float currentTime = 0.0f;

private void Init()
{
// Задаем начальное состояние игрового процесса
state = GameState.Gameplay;
}

private void Update()
{
// если игра окончена
if (state != GameState.Gameplay)
{
// переключаемся на камеру для финала игры
Game.Player = EndCamera;
// показываем сообщение об итоге игры в HUD
ComponentSystem.FindComponentInWorld<HUD>().DisplayStateMessage(state);
}
else
{
// если врагов больше не осталось, переходим в состояние “Победа” (Win)
Log.MessageLine(spawned_enemy_counter + NumEnemies);
if (!ComponentSystem.FindComponentInWorld<EnemyLogic>() && spawned_enemy_counter == NumEnemies)
state = GameState.Win;
// генерируем новых врагов (enemyPrefab) в заданной точке (SpawnPoint)
// с заданным интервалом времени (spawnInterval)
if (spawned_enemy_counter < NumEnemies)
{
currentTime += Game.IFps;

if (currentTime > spawnInterval)
{
currentTime -= spawnInterval;
spawned_enemy_counter++;
Node enemy = World.LoadNode(enemyPrefab.AbsolutePath);
enemy.WorldTransform = SpawnPoint.WorldTransform;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<asset version="2.18.0.1">
<guid>15d59b2cd4cb06278395d79e61d6ece3cb0bdf06</guid>
<type>component</type>
<hash>344bfb97</hash>
<runtimes>
<runtime id="15d59b2cd4cb06278395d79e61d6ece3cb0bdf06" name="GameController.cs" link="0"/>
<runtime id="aa9f1fea1e56f7a6da31878c2789e44171c877b4" name="GameController.prop" link="1" type="4"/>
</runtimes>
</asset>
15 changes: 15 additions & 0 deletions data/csharp_template/FP_controller/components/HUD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,19 @@ public void UpdateHealthInfo(int health)
{
label.Text = "Health: " + health.ToString();
}

// обновление текущего уровня здоровья игрока
public void DisplayStateMessage(GameState state)
{
// добавляем виджет WidgetLabel для отображения финального сообщение о результате игры, устанавливаем размер и цвет шрифта
WidgetLabel end_message = new WidgetLabel(screenGui, (state == GameState.Win) ? "ПОБЕДА!" : "ВЫ ПРОИГРАЛИ!");
end_message.FontSize = 100;
end_message.FontColor = vec4.RED;
screenGui.AddChild(end_message, Gui.ALIGN_CENTER | Gui.ALIGN_OVERLAP);
// привязываем время жизни виджета к миру
end_message.Lifetime = Widget.LIFETIME.WORLD;

// завершаем процесс
ComponentSystem.FindComponentInWorld<GameController>().Enabled = false;
}
}
2 changes: 1 addition & 1 deletion data/csharp_template/FP_controller/components/HUD.cs.meta
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<asset version="2.18.0.1">
<guid>5faca71f8446cfac3ee60b883c3368aef60d5fab</guid>
<type>component</type>
<hash>3964a462</hash>
<hash>5c80a8e7</hash>
<runtimes>
<runtime id="5faca71f8446cfac3ee60b883c3368aef60d5fab" name="HUD.cs" link="0"/>
<runtime id="6b62a5653e3463f1423407e64c4abca756dadd51" name="HUD.prop" link="1" type="4"/>
Expand Down
15 changes: 14 additions & 1 deletion data/csharp_template/FP_controller/components/PlayerLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,36 @@
public class PlayerLogic : Component
{
private Health health = null;
private GameController gameController = null;

private void Init()
{
// берем у ноды компонент Health
health = node.GetComponentInChildren<Health>();
// обновляем информацию об исходном здоровье игрока
ComponentSystem.FindComponentInWorld<HUD>().UpdateHealthInfo(health.health);

// находим компонент GameController
gameController = ComponentSystem.FindComponentInWorld<GameController>();
}

private void Update()
{
// проверяем выставлен ли флаг IsDead
if (health && health.IsDead)
{
// обездвиживаем игрока, отключая компоненты
node.GetComponent<FirstPersonController>().Enabled = false;
node.GetComponent<WeaponController>().Enabled = false;
node.GetComponent<ShootInput>().Enabled = false;

// удаляем игрока
node.DeleteLater();

// меняем состояние игрового процесса на (Lose - поражение)
gameController.state = GameState.Lose;
}
// проверяем состояние игры, если она окончена, удаляем игрока
else if (gameController.state != GameState.Gameplay)
node.DeleteLater();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<asset version="2.18.0.1">
<guid>c70564d6d805b75f7c23b858d839b1091b4f9c80</guid>
<type>component</type>
<hash>8602d67f</hash>
<hash>e6f4e5f9</hash>
<runtimes>
<runtime id="c70564d6d805b75f7c23b858d839b1091b4f9c80" name="PlayerLogic.cs" link="0"/>
<runtime id="96c240040ff7888aa3804c36b43a466e1b430b9e" name="PlayerLogic.prop" link="1" type="4"/>
Expand Down
2 changes: 2 additions & 0 deletions data/guids.db
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@
"cb3839fe5ac15e0145621fbc772e1e9caa5a4247": "fps/components/Lifetime.cs.meta",
"2ecdc69731f9baff81a29a3d4f3df1ea5db785af": "csharp_template/FP_controller/components/Health.cs.meta",
"b27eae5f554475bb72d78fc8446f0ebec0ed12eb": "csharp_template/FP_controller/components/VFXController.cs.meta",
"9795643eab874b2a460ce200bc73663ce99c8c28": "csharp_template/FP_controller/components/GameController.cs.meta",
"250dcf5fc4592814fceeb4c89d5c69e19dd8a3eb": "csharp_template/FP_controller/components/EnemyFireController.cs.meta",
"117116973d68f9d5d633e5a255fb6f67627b61ee": "csharp_template/FP_controller/components/HandAnimationController.cs.meta",
"1e9d3c93b8b196fb5847ecccb5871f542bef2adf": "csharp_template/FP_controller/components/WeaponController.cs.meta",
Expand Down Expand Up @@ -2056,6 +2057,7 @@
"4aed6f037c03c8869f9b862d692fb1a8ee0be8e7": "fps/components/Lifetime.cs",
"6629c61dbcf5668c3f0966d3e2f41c71b0b5b1b5": "csharp_template/FP_controller/components/Health.cs",
"b23baaa53cabf948b0118226ccc00803ae73be91": "csharp_template/FP_controller/components/VFXController.cs",
"15d59b2cd4cb06278395d79e61d6ece3cb0bdf06": "csharp_template/FP_controller/components/GameController.cs",
"4b3823a5c4fc24a3c73f5f268369b30430346791": "csharp_template/FP_controller/components/EnemyFireController.cs",
"4e28861d843434e837504ef0429bf99b43420c5c": "csharp_template/FP_controller/components/HandAnimationController.cs",
"1005e2b9f814eb840a73c08a2aabf58cc6fbbb65": "csharp_template/FP_controller/components/WeaponController.cs",
Expand Down

0 comments on commit 7de38b7

Please sign in to comment.