/// <summary> /// Constructor for the player class that gets the client and it's id. /// </summary> /// <param name="client"> /// The client as a ICanBeNotified type. /// </param> /// <param name="id"> /// The id of the player. /// </param> public Player(ICanbeNotified client, int id) { this.client = client; this.id = id; // Initialize as empty NotifyMeWhenYouMove = new MoveListener((s) => { }); format = (dir) => ""; }
// Update is called once per frame void FixedUpdate() { if (lerpFraction < 1) { transform.position = Vector3.Lerp(movingFrom, movingTo, lerpFraction); lerpFraction += frameStep; if (lerpFraction < 1) { return; } if (MoveListener != null) { MoveListener.Invoke((int)movingTo.x, (int)movingTo.y); } } GridPosition move = AI.GetMove(); if (move.x == 0 && move.y == 0) { transform.position = movingTo; lerpFraction = 1; return; } movingFrom = transform.position; if (move.x == 0) { movingFrom.x = (float)Math.Round(transform.position.x); } if (move.y == 0) { movingFrom.y = (float)Math.Round(transform.position.y); } movingTo = new Vector3((float)Math.Round(movingFrom.x) + move.x, (float)Math.Round(movingFrom.y) + move.y); lerpFraction = 0; if (Rotate) { int[] degrees = new int[] { -1, 180, -1, 270, -1, 90, -1, 0, -1 }; int dirId = move.x * 3 + move.y + 4; transform.rotation = Quaternion.Euler(0, 0, degrees[(int)dirId]); } }
// Reallocate public bool ReAlloc(Range allocation, int newSize, bool moveable, bool allocDefrag) { // Otherwise we need a move handler if (MoveListener == null) { throw new InvalidOperationException("RangeAllocator needs a move handler"); } // Get the entry var e = allocation as Entry; int index = IndexOfAllocation(e); // If changing moveable type then alway Defrag and move to get it in the best position if (moveable != e.moveable) { // Realloc with a new entry if (ReallocWithNewEntry(e, newSize, moveable)) { return(true); } // Defrag allowed? if (!allocDefrag) { return(false); } // Try Defraging and creating the new entry return(DefragAndRealloc(e, index, newSize, moveable)); } // Try to do it in place (this will handle shrink + grow to following free space) if (ReAllocInPlace(allocation, newSize)) { return(true); } // We must be growing Debug.Assert(newSize > e.size); // Try to use preceeding space int localSpace = _entries[index].size; if (index > 0 && !_entries[index - 1].allocated) { var eBefore = _entries[index - 1]; localSpace += eBefore.size; // Do we have room? if (localSpace >= newSize) { // Work out new position (overlap end of preceeding free entry) int newPosition = eBefore.position + localSpace - newSize; // Move data MoveListener.Move(e, newPosition); // Make as allocated eBefore.size = newPosition - eBefore.position; // Update free space _freeSpace -= (newSize - e.size); // Conver the old allocation to a free list entry e.position = newPosition; e.size = newSize; // Remove the preceeding free entry if not used if (eBefore.size == 0) { _entries.RemoveAt(index - 1); } Check(); return(true); } } // Try to use preceeding and following space if (index + 1 < _entries.Count && !_entries[index + 1].allocated) { localSpace += _entries[index + 1].size; if (localSpace > newSize) { var eBefore = _entries[index - 1]; var eAfter = _entries[index + 1]; // We're using all the prior entry and all the current entry and some of the following Debug.Assert(index >= 1); Debug.Assert(newSize > eBefore.size + e.size); // Move the data MoveListener.Move(e, eBefore.position); // Update the following entry eAfter.position += newSize - (eBefore.size + e.size); eAfter.size -= newSize - (eBefore.size + e.size); System.Diagnostics.Debug.Assert(eAfter.size >= 0); if (eAfter.size == 0) { _entries.RemoveAt(index + 1); } // Remove the preceeding entry _entries.RemoveAt(index - 1); // Update the free space _freeSpace -= newSize - e.size; // Update the current entry e.size = newSize; e.position = eBefore.position; // Done! Check(); return(true); } } if (ReallocWithNewEntry(e, newSize, moveable)) { return(true); } // Allow Defragion? if (!allocDefrag) { return(false); } // Do full Defrag and realloc return(DefragAndRealloc(e, index, newSize, moveable)); }
public void RemoveMoveListener(MoveListener ML) { _moved -= ML; }
public void addMoveListener(MoveListener ML) { _moved += ML; }