1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Text.Json;
- namespace BubbleSocketCore.Input
- {
- public class InputFrame
- {
- public int id; //frame id
- public ConcurrentDictionary<int, List<PlayerInput>> inputData = new ConcurrentDictionary<int, List<PlayerInput>>(); //entityId
- public InputFrame(int id)
- {
- this.id = id;
- }
- override public string ToString()
- {
- return JsonSerializer.Serialize(Serialize());
- }
- public Dictionary<string, object> Serialize()
- {
- var data = new Dictionary<string, object>();
- foreach (KeyValuePair<int, List<PlayerInput>> item in inputData)
- {
- var inputs = new List<List<string>>();
- item.Value.ForEach(input =>
- {
- inputs.Add(input.Serialize());
- });
- data.Add($"{item.Key}", inputs);
- }
- return data;
- }
- public void Add(int entityId, List<PlayerInput> input)
- {
- inputData.TryAdd(entityId, new List<PlayerInput> { });
- }
- public InputFrame Clone()
- {
- var clonedFrame = new InputFrame(this.id);
- var data = new Dictionary<int, object>();
- foreach (KeyValuePair<int, List<PlayerInput>> item in inputData)
- {
- var inputs = new List<PlayerInput>();
- item.Value.ForEach(input =>
- {
- inputs.Add(input.Clone());
- });
- clonedFrame.Add(item.Key, inputs);
- }
- return clonedFrame;
- }
- public List<PlayerInput> GetInputForEntity(int key)
- {
- List<PlayerInput> possibleInputs;
- inputData.TryGetValue(key, out possibleInputs);
- if (possibleInputs == null)
- {
- return new List<PlayerInput>();
- }
- return possibleInputs;
- }
- }
- }
|