-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
487 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// <copyright file="GateNpc.cs" company="MUnique"> | ||
// Licensed under the MIT License. See LICENSE file in the project root for full license information. | ||
// </copyright> | ||
|
||
namespace MUnique.OpenMU.GameLogic.NPC; | ||
|
||
using System.Threading; | ||
|
||
/// <summary> | ||
/// Represents a <see cref="NonPlayerCharacter"/> which is a gate to another map. | ||
/// When a player gets close to the gate, it will warp the player to the target gate, | ||
/// if the player is in the same party as the summoner. | ||
/// </summary> | ||
public class GateNpc : NonPlayerCharacter, ISummonable | ||
{ | ||
private const int Range = 2; | ||
private readonly ILogger<GateNpc> _logger; | ||
private readonly Task _warpTask; | ||
private readonly CancellationTokenSource _cts; | ||
private int _enterCount; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="GateNpc" /> class. | ||
/// </summary> | ||
/// <param name="spawnInfo">The spawn information.</param> | ||
/// <param name="stats">The stats.</param> | ||
/// <param name="map">The map.</param> | ||
/// <param name="summonedBy">The summoned by.</param> | ||
/// <param name="targetGate">The target gate.</param> | ||
/// <param name="lifespan">The lifespan.</param> | ||
public GateNpc(MonsterSpawnArea spawnInfo, MonsterDefinition stats, GameMap map, Player summonedBy, ExitGate targetGate, TimeSpan lifespan) | ||
: base(spawnInfo, stats, map) | ||
{ | ||
this.SummonedBy = summonedBy; | ||
this.TargetGate = targetGate; | ||
this._logger = summonedBy.GameContext.LoggerFactory.CreateLogger<GateNpc>(); | ||
this._cts = new CancellationTokenSource(lifespan); | ||
this._warpTask = Task.Run(this.RunTaskAsync); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public Player SummonedBy { get; } | ||
|
||
/// <summary> | ||
/// Gets the target gate. | ||
/// </summary> | ||
public ExitGate TargetGate { get; } | ||
|
||
/// <inheritdoc /> | ||
protected override async ValueTask DisposeAsyncCore() | ||
{ | ||
await this._cts.CancelAsync().ConfigureAwait(false); | ||
this._cts.Dispose(); | ||
|
||
await this._warpTask.ConfigureAwait(false); | ||
await base.DisposeAsyncCore().ConfigureAwait(false); | ||
} | ||
|
||
private async Task RunTaskAsync() | ||
{ | ||
try | ||
{ | ||
var cancellationToken = this._cts.Token; | ||
var playersInRange = new List<Player>(); | ||
while (true) | ||
{ | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
|
||
await Task.Delay(1000, cancellationToken).ConfigureAwait(false); | ||
|
||
if (!this.SummonedBy.IsAlive) | ||
{ | ||
this._logger.LogInformation("Closing the gate, player is dead or offline"); | ||
return; | ||
} | ||
|
||
playersInRange.Clear(); | ||
if (this.SummonedBy.Party is { } party) | ||
{ | ||
playersInRange.AddRange(party.PartyList.OfType<Player>().Where(p => p.IsActive() && p.IsInRange(this, Range) && this.CurrentMap == p.CurrentMap)); | ||
} | ||
else if (this.SummonedBy.IsActive() && this.SummonedBy.IsInRange(this, Range) && this.CurrentMap == this.SummonedBy.CurrentMap) | ||
{ | ||
playersInRange.Add(this.SummonedBy); | ||
} | ||
|
||
foreach (var player in playersInRange) | ||
{ | ||
this._logger.LogInformation("Player {player} passes the gate ({enterCount})", player, this._enterCount); | ||
await player.WarpToAsync(this.TargetGate).ConfigureAwait(false); | ||
this._enterCount++; | ||
if (this._enterCount >= this.SummonedBy.GameContext.Configuration.MaximumPartySize) | ||
{ | ||
this._logger.LogInformation("Closing the gate, maximum entrances reached ({enterCount})", this._enterCount); | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
// ignored | ||
} | ||
catch (Exception ex) | ||
{ | ||
this._logger.LogError(ex, "Error in GateNpc task."); | ||
} | ||
finally | ||
{ | ||
await this.DisposeAsync().ConfigureAwait(false); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// <copyright file="ISummonable.cs" company="MUnique"> | ||
// Licensed under the MIT License. See LICENSE file in the project root for full license information. | ||
// </copyright> | ||
|
||
namespace MUnique.OpenMU.GameLogic.NPC; | ||
|
||
/// <summary> | ||
/// An interface for a class which can (but not must) be summoned by a player. | ||
/// </summary> | ||
public interface ISummonable : IIdentifiable, ILocateable, IRotatable | ||
{ | ||
/// <summary> | ||
/// Gets the player which summoned this instance. | ||
/// </summary> | ||
Player? SummonedBy { get; } | ||
|
||
/// <summary> | ||
/// Gets the definition for this instance. | ||
/// </summary> | ||
MonsterDefinition Definition { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// <copyright file="ItemBoxDroppedPlugIn.cs" company="MUnique"> | ||
// Licensed under the MIT License. See LICENSE file in the project root for full license information. | ||
// </copyright> | ||
|
||
namespace MUnique.OpenMU.GameLogic.PlayerActions.Items; | ||
|
||
using System.Runtime.InteropServices; | ||
using MUnique.OpenMU.GameLogic.PlugIns; | ||
using MUnique.OpenMU.GameLogic.Views.Character; | ||
using MUnique.OpenMU.Pathfinding; | ||
using MUnique.OpenMU.PlugIns; | ||
using static MUnique.OpenMU.GameLogic.PlugIns.IItemDropPlugIn; | ||
|
||
/// <summary> | ||
/// This plugin handles the drop of an item box, e.g. box of kundun. | ||
/// </summary> | ||
[PlugIn(nameof(ItemBoxDroppedPlugIn), "This plugin handles the drop of an item box, e.g. box of kundun.")] | ||
[Guid("3D15D55D-EEFE-4B5F-89B1-6934AB3F0BEE")] | ||
public sealed class ItemBoxDroppedPlugIn : IItemDropPlugIn | ||
{ | ||
/// <inheritdoc /> | ||
public async ValueTask HandleItemDropAsync(Player player, Item sourceItem, Point target, ItemDropArguments cancelArgs) | ||
{ | ||
if (sourceItem.Definition!.DropItems.Count <= 0 | ||
|| sourceItem.Definition!.DropItems.Where(di => di.SourceItemLevel == sourceItem.Level) is not { } itemDropGroups) | ||
{ | ||
return; | ||
} | ||
|
||
cancelArgs.WasHandled = true; | ||
if (itemDropGroups.Any(g => g.RequiredCharacterLevel > player.Level)) | ||
{ | ||
cancelArgs.Success = false; | ||
return; | ||
} | ||
|
||
cancelArgs.Success = true; | ||
var (item, droppedMoneyAmount, dropEffect) = player.GameContext.DropGenerator.GenerateItemDrop(itemDropGroups); | ||
if (droppedMoneyAmount is not null) | ||
{ | ||
var droppedMoney = new DroppedMoney(droppedMoneyAmount.Value, player.Position, player.CurrentMap!); | ||
await player.CurrentMap!.AddAsync(droppedMoney).ConfigureAwait(false); | ||
} | ||
|
||
if (item is not null) | ||
{ | ||
var droppedItem = new DroppedItem(item, player.Position, player.CurrentMap!, player); | ||
await player.CurrentMap!.AddAsync(droppedItem).ConfigureAwait(false); | ||
} | ||
|
||
if (dropEffect is not ItemDropEffect.Undefined) | ||
{ | ||
await this.ShowDropEffectAsync(player, dropEffect).ConfigureAwait(false); | ||
} | ||
} | ||
|
||
private async ValueTask ShowDropEffectAsync(Player player, ItemDropEffect dropEffect) | ||
{ | ||
if (dropEffect == ItemDropEffect.Swirl) | ||
{ | ||
await player.InvokeViewPlugInAsync<IShowEffectPlugIn>(p => p.ShowEffectAsync(player, IShowEffectPlugIn.EffectType.Swirl)).ConfigureAwait(false); | ||
} | ||
else | ||
{ | ||
await player.InvokeViewPlugInAsync<IShowItemDropEffectPlugIn>(p => p.ShowEffectAsync(dropEffect, player.Position)).ConfigureAwait(false); | ||
} | ||
} | ||
} |
Oops, something went wrong.