feat: add creatures and drops on the map

This commit is contained in:
k0t9i
2023-02-07 22:21:15 +04:00
parent d03f37fbf7
commit 2480563914
13 changed files with 529 additions and 59 deletions

View File

@ -0,0 +1,126 @@
using Client.Domain.Common;
using Client.Domain.Entities;
using Client.Domain.Enums;
using Client.Domain.ValueObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Client.Application.ViewModels
{
public class CreatureMapViewModel : ObservableObject
{
public uint Id => creature.Id;
public string Name => creature.Name;
public Vector3 Position => new Vector3(
(creature.Transform.Position.X - hero.Transform.Position.X) / scale + (VieportSize.X / 2),
(creature.Transform.Position.Y - hero.Transform.Position.Y) / scale + (VieportSize.Y / 2),
0
);
public Vector3 Direction => new Vector3(
creature.Transform.Direction.X * Radius * 2f,
creature.Transform.Direction.Y * Radius * 2f,
0
);
public VitalStats VitalStats => creature.VitalStats;
public float Radius => MathF.Max(MAX_RADIUS / scale, MIN_RADIUS);
public float Scale
{
get => scale;
set
{
if (scale != value)
{
scale = value;
OnPropertyChanged("Position");
OnPropertyChanged("Direction");
OnPropertyChanged("Radius");
OnPropertyChanged("AggroRadius");
}
}
}
public Vector3 VieportSize
{
get => vieportSize;
set
{
if (vieportSize != value)
{
vieportSize = value;
OnPropertyChanged("Position");
OnPropertyChanged("Direction");
}
}
}
public CreatureTypeEnum Type => creature.Type;
public bool IsTarget => Id == hero.TargetId;
public bool IsAggressive => creature.AggroRadius > 0;
public float AggroRadius => creature.AggroRadius / scale;
public CreatureMapViewModel(CreatureInterface creature, Hero hero)
{
this.creature = creature;
this.hero = hero;
creature.PropertyChanged += Creature_PropertyChanged;
creature.Transform.PropertyChanged += Transform_PropertyChanged;
creature.Transform.Position.PropertyChanged += Position_PropertyChanged;
creature.VitalStats.PropertyChanged += VitalStats_PropertyChanged;
hero.Transform.Position.PropertyChanged += HeroPosition_PropertyChanged;
hero.PropertyChanged += Hero_PropertyChanged;
}
private void VitalStats_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "Hp" || e.PropertyName == "MaxHp")
{
OnPropertyChanged("VitalStats");
}
}
private void Hero_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "TargetId")
{
OnPropertyChanged("IsTarget");
}
}
private void HeroPosition_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
OnPropertyChanged("Position");
OnPropertyChanged("Direction");
}
private void Position_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
OnPropertyChanged("Position");
OnPropertyChanged("Direction");
}
private void Transform_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "Direction")
{
OnPropertyChanged("Direction");
}
}
private void Creature_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "Name")
{
OnPropertyChanged("Name");
}
}
private readonly CreatureInterface creature;
private readonly Hero hero;
private float scale = 1;
private static readonly float MAX_RADIUS = 10;
private static readonly float MIN_RADIUS = 4;
private Vector3 vieportSize = new Vector3(0, 0, 0);
}
}

View File

@ -0,0 +1,84 @@
using Client.Domain.Common;
using Client.Domain.Entities;
using Client.Domain.Enums;
using Client.Domain.ValueObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
namespace Client.Application.ViewModels
{
public class DropMapViewModel : ObservableObject
{
public uint Id => drop.Id;
public string Name => drop.Name;
public Vector3 Position => new Vector3(
(drop.Transform.Position.X - hero.Transform.Position.X) / scale + (VieportSize.X / 2),
(drop.Transform.Position.Y - hero.Transform.Position.Y) / scale + (VieportSize.Y / 2),
0
);
public float Radius => MathF.Max(MAX_RADIUS / scale, MIN_RADIUS);
public float Scale
{
get => scale;
set
{
if (scale != value)
{
scale = value;
OnPropertyChanged("Position");
OnPropertyChanged("Radius");
}
}
}
public Vector3 VieportSize
{
get => vieportSize;
set
{
if (vieportSize != value)
{
vieportSize = value;
OnPropertyChanged("Position");
}
}
}
public DropMapViewModel(Drop drop, Hero hero)
{
this.drop = drop;
this.hero = hero;
drop.PropertyChanged += Creature_PropertyChanged;
drop.Transform.Position.PropertyChanged += Position_PropertyChanged;
hero.Transform.Position.PropertyChanged += HeroPosition_PropertyChanged;
}
private void HeroPosition_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
OnPropertyChanged("Position");
}
private void Position_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
OnPropertyChanged("Position");
}
private void Creature_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "Name")
{
OnPropertyChanged("Name");
}
}
private readonly Drop drop;
private readonly Hero hero;
private float scale = 1;
private static readonly float MAX_RADIUS = 8;
private static readonly float MIN_RADIUS = 2;
private Vector3 vieportSize = new Vector3(0, 0, 0);
}
}

View File

@ -35,12 +35,17 @@ namespace Client.Application.ViewModels
Hero = new HeroSummaryInfoViewModel(@event.Hero);
hero = @event.Hero;
Map.Hero = hero;
AddCreature(hero);
OnPropertyChanged("Hero");
OnPropertyChanged("Map");
}
public void Handle(HeroDeletedEvent @event)
{
if (hero != null)
{
RemoveCreature(hero.Id);
}
Hero = null;
hero = null;
Map.Hero = null;
@ -53,12 +58,14 @@ namespace Client.Application.ViewModels
if (hero != null)
{
Creatures.Add(new CreatureListViewModel(@event.Creature, hero));
AddCreature(@event.Creature);
}
}
public void Handle(CreatureDeletedEvent @event)
{
Creatures.RemoveAll(x => x.Id == @event.Id);
RemoveCreature(@event.Id);
}
public void Handle(DropCreatedEvent @event)
@ -66,12 +73,14 @@ namespace Client.Application.ViewModels
if (hero != null)
{
Drops.Add(new DropListViewModel(@event.Drop, hero));
Map.Drops.Add(new DropMapViewModel(@event.Drop, hero));
}
}
public void Handle(DropDeletedEvent @event)
{
Drops.RemoveAll(x => x.Id == @event.Id);
Map.Drops.RemoveAll(x => x.Id == @event.Id);
}
public void Handle(ChatMessageCreatedEvent @event)
@ -113,6 +122,19 @@ namespace Client.Application.ViewModels
Items.RemoveAll(x => x.Id == @event.Id);
}
private void AddCreature(CreatureInterface creature)
{
if (hero != null)
{
Map.Creatures.Add(new CreatureMapViewModel(creature, hero));
}
}
private void RemoveCreature(uint id)
{
Map.Creatures.RemoveAll(x => x.Id == id);
}
public ObservableCollection<ChatMessageViewModel> ChatMessages { get; } = new ObservableCollection<ChatMessageViewModel>();
public ObservableCollection<CreatureListViewModel> Creatures { get; } = new ObservableCollection<CreatureListViewModel>();
public ObservableCollection<DropListViewModel> Drops { get; } = new ObservableCollection<DropListViewModel>();

View File

@ -8,7 +8,9 @@ using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Collections.Specialized;
namespace Client.Application.ViewModels
{
@ -35,44 +37,6 @@ namespace Client.Application.ViewModels
}
}
private void HeroPosition_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
UpdateMap();
}
private void UpdateMap()
{
foreach (var block in Blocks)
{
block.Visible = false;
}
if (hero != null)
{
var blocks = selector.SelectImages((float)ViewportWidth, (float)ViewportHeight, hero.Transform.Position, Scale);
foreach (var block in blocks)
{
if (this.blocks.ContainsKey(block.Id))
{
this.blocks[block.Id].MapBlock.DeltaX = block.DeltaX;
this.blocks[block.Id].MapBlock.DeltaY = block.DeltaY;
this.blocks[block.Id].MapBlock.Size = block.Size;
}
else
{
var model = new MapBlockViewModel(block);
this.blocks.Add(block.Id, model);
Blocks.Add(model);
}
this.blocks[block.Id].Visible = true;
}
}
}
public ObservableCollection<MapBlockViewModel> Blocks { get; } = new ObservableCollection<MapBlockViewModel>();
public double ViewportWidth
{
get => viewportWidth;
@ -112,11 +76,96 @@ namespace Client.Application.ViewModels
}
}
MapImageSelector selector = new MapImageSelector();
Dictionary<uint, MapBlockViewModel> blocks = new Dictionary<uint, MapBlockViewModel>();
private void HeroPosition_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
UpdateMap();
}
private void UpdateMap()
{
foreach (var block in Blocks)
{
block.Visible = false;
}
if (hero != null)
{
var blocks = selector.SelectImages((float)ViewportWidth, (float)ViewportHeight, hero.Transform.Position, Scale);
foreach (var block in blocks)
{
if (this.blocks.ContainsKey(block.Id))
{
this.blocks[block.Id].MapBlock.DeltaX = block.DeltaX;
this.blocks[block.Id].MapBlock.DeltaY = block.DeltaY;
this.blocks[block.Id].MapBlock.Size = block.Size;
}
else
{
var model = new MapBlockViewModel(block);
this.blocks.Add(block.Id, model);
Blocks.Add(model);
}
this.blocks[block.Id].Visible = true;
}
foreach (var creature in Creatures)
{
creature.Scale = scale;
creature.VieportSize = new Vector3((float)ViewportWidth, (float)ViewportHeight, 0);
}
foreach (var drop in Drops)
{
drop.Scale = scale;
drop.VieportSize = new Vector3((float)ViewportWidth, (float)ViewportHeight, 0);
}
}
}
public MapViewModel()
{
Creatures.CollectionChanged += Creatures_CollectionChanged;
Drops.CollectionChanged += Drops_CollectionChanged;
}
private void Drops_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add && e.NewItems != null)
{
foreach (var item in e.NewItems)
{
var creature = (DropMapViewModel)item;
creature.Scale = scale;
creature.VieportSize = new Vector3((float)ViewportWidth, (float)ViewportHeight, 0);
}
}
}
private void Creatures_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add && e.NewItems != null)
{
foreach (var item in e.NewItems)
{
var creature = (CreatureMapViewModel)item;
creature.Scale = scale;
creature.VieportSize = new Vector3((float)ViewportWidth, (float)ViewportHeight, 0);
}
}
}
public ObservableCollection<MapBlockViewModel> Blocks { get; } = new ObservableCollection<MapBlockViewModel>();
public ObservableCollection<CreatureMapViewModel> Creatures { get; } = new ObservableCollection<CreatureMapViewModel>();
public ObservableCollection<DropMapViewModel> Drops { get; } = new ObservableCollection<DropMapViewModel>();
private MapImageSelector selector = new MapImageSelector();
private Dictionary<uint, MapBlockViewModel> blocks = new Dictionary<uint, MapBlockViewModel>();
private Hero? hero;
private float scale = 8;
private double viewportWidth = 50;
private double viewportHeight = 50;
private double viewportWidth = 0;
private double viewportHeight = 0;
}
}