+ character input keys in array instead of separate variables
This commit is contained in:
Regalis11
2015-08-22 16:57:57 +03:00
102 changed files with 1847 additions and 709 deletions
@@ -43,7 +43,7 @@ namespace Subsurface
steeringManager = new SteeringManager(this);
}
public virtual void SelectTarget(IDamageable target) { }
public virtual void SelectTarget(AITarget target) { }
public virtual void Update(float deltaTime) { }
@@ -82,11 +82,10 @@ namespace Subsurface
state = AiState.None;
}
public override void SelectTarget(IDamageable target)
public override void SelectTarget(AITarget target)
{
targetEntity = target;
selectedAiTarget = target.AiTarget;
selectedTargetMemory = FindTargetMemory(target.AiTarget);
selectedAiTarget = target;
selectedTargetMemory = FindTargetMemory(target);
targetValue = 100.0f;
}
@@ -470,14 +469,21 @@ namespace Subsurface
public override void FillNetworkData(NetOutgoingMessage message)
{
message.Write((byte)state);
bool wallAttack = (wallAttackPos!=Vector2.Zero && state == AiState.Attack);
message.Write(wallAttackPos.X);
message.Write(wallAttackPos.Y);
message.Write(wallAttack);
message.Write(steeringManager.WanderAngle);
message.Write(updateTargetsTimer);
message.Write(raycastTimer);
message.Write(coolDownTimer);
if (wallAttack)
{
message.Write(wallAttackPos.X);
message.Write(wallAttackPos.Y);
}
message.Write(MathUtils.AngleToByte(steeringManager.WanderAngle));
message.WriteRangedSingle(MathHelper.Clamp(updateTargetsTimer,0.0f, UpdateTargetsInterval), 0.0f, UpdateTargetsInterval, 8);
message.WriteRangedSingle(MathHelper.Clamp(raycastTimer, 0.0f, RaycastInterval), 0.0f, RaycastInterval, 8);
message.WriteRangedSingle(MathHelper.Clamp(coolDownTimer, 0.0f, attackCoolDown * 2.0f), 0.0f, attackCoolDown * 2.0f, 8);
message.Write(targetEntity==null ? -1 : (targetEntity as Entity).ID);
}
@@ -485,7 +491,7 @@ namespace Subsurface
public override void ReadNetworkData(NetIncomingMessage message)
{
AiState newState = AiState.None;
Vector2 newWallAttackPos;
Vector2 newWallAttackPos = Vector2.Zero;
float wanderAngle;
float updateTargetsTimer, raycastTimer, coolDownTimer;
@@ -495,12 +501,18 @@ namespace Subsurface
{
newState = (AiState)(message.ReadByte());
newWallAttackPos = new Vector2(message.ReadFloat(), message.ReadFloat());
wanderAngle = MathUtils.WrapAngleTwoPi(message.ReadFloat());
updateTargetsTimer = MathHelper.Clamp(message.ReadFloat(), 0.0f, UpdateTargetsInterval);
raycastTimer = MathHelper.Clamp(message.ReadFloat(), 0.0f, RaycastInterval);
coolDownTimer = MathHelper.Clamp(message.ReadFloat(), 0.0f, attackCoolDown);
bool wallAttack = message.ReadBoolean();
if (wallAttack)
{
newWallAttackPos = new Vector2(message.ReadFloat(), message.ReadFloat());
}
wanderAngle = MathUtils.ByteToAngle(message.ReadByte());
updateTargetsTimer = message.ReadRangedSingle(0.0f, UpdateTargetsInterval, 8);
raycastTimer = message.ReadRangedSingle(0.0f, RaycastInterval, 8);
coolDownTimer = message.ReadRangedSingle(0.0f, attackCoolDown*2.0f, 8);
targetID = message.ReadInt32();
}
@@ -31,7 +31,11 @@ namespace Subsurface
public float StunTimer
{
get { return stunTimer; }
set { stunTimer = value; }
set
{
if (float.IsNaN(value) || float.IsInfinity(value)) return;
stunTimer = value;
}
}
public AnimController(Character character, XElement element)
+101 -98
View File
@@ -36,7 +36,7 @@ namespace Subsurface
private CharacterInventory inventory;
public double LastNetworkUpdate;
public float LastNetworkUpdate;
public int LargeUpdateTimer;
@@ -46,9 +46,11 @@ namespace Subsurface
get { return Properties; }
}
protected Key selectKeyHit;
protected Key actionKeyHit, actionKeyDown;
protected Key secondaryKeyHit, secondaryKeyDown;
protected Key[] keys;
//protected Key selectKeyHit;
//protected Key actionKeyHit, actionKeyDown;
//protected Key secondaryKeyHit, secondaryKeyDown;
private Item selectedConstruction;
private Item[] selectedItems;
@@ -244,31 +246,6 @@ namespace Subsurface
get { return closestItem; }
}
public Key SelectKeyHit
{
get { return selectKeyHit; }
}
public Key ActionKeyHit
{
get { return actionKeyHit; }
}
public Key ActionKeyDown
{
get { return actionKeyDown; }
}
public Key SecondaryKeyHit
{
get { return secondaryKeyHit; }
}
public Key SecondaryKeyDown
{
get { return secondaryKeyDown; }
}
public AIController AIController
{
get { return aiController; }
@@ -311,12 +288,13 @@ namespace Subsurface
public Character(string file, Vector2 position, CharacterInfo characterInfo = null, bool isNetworkPlayer = false)
{
selectKeyHit = new Key(false);
actionKeyDown = new Key(true);
actionKeyHit = new Key(false);
secondaryKeyHit = new Key(false);
secondaryKeyDown = new Key(true);
keys = new Key[5];
keys[(int)InputType.Select] = new Key(false);
keys[(int)InputType.ActionHeld] = new Key(true);
keys[(int)InputType.ActionHit] = new Key(false);
keys[(int)InputType.SecondaryHit] = new Key(false);
keys[(int)InputType.SecondaryHeld] = new Key(true);
selectedItems = new Item[2];
IsNetworkPlayer = isNetworkPlayer;
@@ -426,6 +404,16 @@ namespace Subsurface
}
}
public bool GetInputState(InputType inputType)
{
return keys[(int)inputType].State;
}
public override string ToString()
{
return (info != null && !string.IsNullOrWhiteSpace(info.Name)) ? info.Name : SpeciesName;
}
public void GiveJobItems(WayPoint spawnPoint)
{
if (info == null || info.Job == null) return;
@@ -482,7 +470,7 @@ namespace Subsurface
if (closestItem != null)
{
closestItem.IsHighlighted = true;
if (selectKeyHit.State && closestItem.Pick(this, forcePick))
if (GetInputState(InputType.Select) && closestItem.Pick(this, forcePick))
{
new NetworkEvent(NetworkEventType.PickItem, ID, true, closestItem.ID);
}
@@ -492,7 +480,7 @@ namespace Subsurface
if (closestCharacter != selectedCharacter) selectedCharacter = null;
if (closestCharacter!=null)
{
if (selectKeyHit.State) selectedCharacter = (selectedCharacter==null) ? closestCharacter : null;
if (GetInputState(InputType.Select)) selectedCharacter = (selectedCharacter == null) ? closestCharacter : null;
}
}
@@ -501,23 +489,22 @@ namespace Subsurface
if (selectedItems[i] == null) continue;
if (i == 1 && selectedItems[0] == selectedItems[1]) continue;
if (actionKeyDown.State) selectedItems[i].Use(deltaTime, this);
if (secondaryKeyDown.State && selectedItems[i] != null) selectedItems[i].SecondaryUse(deltaTime, this);
if (GetInputState(InputType.ActionHeld)) selectedItems[i].Use(deltaTime, this);
if (GetInputState(InputType.SecondaryHeld) && selectedItems[i] != null) selectedItems[i].SecondaryUse(deltaTime, this);
}
if (selectedConstruction != null)
{
if (actionKeyDown.State) selectedConstruction.Use(deltaTime, this);
if (secondaryKeyDown.State) selectedConstruction.SecondaryUse(deltaTime, this);
if (GetInputState(InputType.ActionHeld)) selectedConstruction.Use(deltaTime, this);
if (GetInputState(InputType.SecondaryHeld)) selectedConstruction.SecondaryUse(deltaTime, this);
}
if (IsNetworkPlayer)
{
selectKeyHit.Reset();
actionKeyHit.Reset();
actionKeyDown.Reset();
secondaryKeyHit.Reset();
secondaryKeyDown.Reset();
foreach (Key key in keys)
{
key.Reset();
}
}
}
@@ -589,19 +576,19 @@ namespace Subsurface
if (Keyboard.GetState().IsKeyDown(Keys.LeftShift) && Math.Sign(targetMovement.X) == Math.Sign(AnimController.Dir))
targetMovement *= 3.0f;
selectKeyHit.SetState(PlayerInput.KeyHit(Keys.E));
actionKeyHit.SetState(PlayerInput.LeftButtonClicked());
actionKeyDown.SetState(PlayerInput.GetMouseState.LeftButton == ButtonState.Pressed);
secondaryKeyHit.SetState(PlayerInput.RightButtonClicked());
secondaryKeyDown.SetState(PlayerInput.GetMouseState.RightButton == ButtonState.Pressed);
keys[(int)InputType.Select].SetState(PlayerInput.KeyHit(Keys.E));
keys[(int)InputType.ActionHit].SetState(PlayerInput.LeftButtonClicked());
keys[(int)InputType.ActionHeld].SetState(PlayerInput.GetMouseState.LeftButton == ButtonState.Pressed);
keys[(int)InputType.SecondaryHit].SetState(PlayerInput.RightButtonClicked());
keys[(int)InputType.SecondaryHeld].SetState(PlayerInput.GetMouseState.RightButton == ButtonState.Pressed);
}
else
{
selectKeyHit.SetState(false);
actionKeyHit.SetState(false);
actionKeyDown.SetState(false);
secondaryKeyHit.SetState(false);
secondaryKeyDown.SetState(false);
foreach (Key key in keys)
{
key.SetState(false);
}
}
AnimController.TargetMovement = targetMovement;
@@ -629,7 +616,6 @@ namespace Subsurface
}
}
if (AnimController.onGround &&
!AnimController.InWater &&
AnimController.Anim != AnimController.Animation.UsingConstruction)
@@ -1020,26 +1006,28 @@ namespace Subsurface
{
return;
}
else if (type== NetworkEventType.NotMoving)
{
return;
}
//if (type == Networking.NetworkEventType.KeyHit)
//{
// message.Write(selectKeyHit.Dequeue);
message.Write(actionKeyDown.Dequeue);
message.Write(secondaryKeyDown.Dequeue);
//}
message.Write(NetTime.Now);
message.Write(keys[(int)InputType.ActionHeld].Dequeue);
message.Write(keys[(int)InputType.SecondaryHeld].Dequeue);
message.Write((float)NetTime.Now);
// Write byte = move direction
message.Write(AnimController.TargetMovement.X);
message.Write(AnimController.TargetMovement.Y);
message.WriteRangedSingle(MathHelper.Clamp(AnimController.TargetMovement.X, -10.0f, 10.0f), -10.0f, 10.0f, 8);
message.WriteRangedSingle(MathHelper.Clamp(AnimController.TargetMovement.Y, -10.0f, 10.0f), -10.0f, 10.0f, 8);
message.Write(AnimController.TargetDir==Direction.Right);
message.Write(cursorPosition.X);
message.Write(cursorPosition.Y);
if (aiController==null)
{
message.Write(cursorPosition.X);
message.Write(cursorPosition.Y);
}
message.Write(LargeUpdateTimer <= 0);
if (LargeUpdateTimer<=0)
@@ -1050,32 +1038,31 @@ namespace Subsurface
message.Write(limb.body.Position.X);
message.Write(limb.body.Position.Y);
message.Write(limb.body.LinearVelocity.X);
message.Write(limb.body.LinearVelocity.Y);
//message.Write(limb.body.LinearVelocity.X);
//message.Write(limb.body.LinearVelocity.Y);
message.Write(limb.body.Rotation);
message.Write(limb.body.AngularVelocity);
//message.WriteRangedSingle(MathHelper.Clamp(limb.body.AngularVelocity, -10.0f, 10.0f), -10.0f, 10.0f, 8);
i++;
}
message.Write(AnimController.StunTimer);
message.Write((byte)health);
message.WriteRangedSingle(MathHelper.Clamp(AnimController.StunTimer,0.0f,60.0f), 0.0f, 60.0f, 8);
message.Write((byte)((health/maxHealth)*255.0f));
LargeUpdateTimer = 5;
if (aiController != null) aiController.FillNetworkData(message);
LargeUpdateTimer = 10;
}
else
{
Limb torso = AnimController.GetLimb(LimbType.Torso);
if (torso == null) torso = AnimController.GetLimb(LimbType.Head);
message.Write(torso.body.Position.X);
message.Write(torso.body.Position.Y);
LargeUpdateTimer = Math.Max(0, LargeUpdateTimer-1);
}
if (aiController != null) aiController.FillNetworkData(message);
}
}
public override void ReadNetworkData(NetworkEventType type, NetIncomingMessage message)
@@ -1112,10 +1099,17 @@ namespace Subsurface
}
return;
}
else if (type == NetworkEventType.NotMoving)
{
AnimController.TargetMovement = Vector2.Zero;
keys[(int)InputType.ActionHeld].State = false;
keys[(int)InputType.SecondaryHeld].State = false;
return;
}
bool actionKeyState = false;
bool secondaryKeyState = false;
double sendingTime = 0.0f;
float sendingTime = 0.0f;
Vector2 targetMovement = Vector2.Zero;
bool targetDir = false;
Vector2 cursorPos = Vector2.Zero;
@@ -1125,12 +1119,20 @@ namespace Subsurface
actionKeyState = message.ReadBoolean();
secondaryKeyState = message.ReadBoolean();
sendingTime = message.ReadDouble();
sendingTime = message.ReadFloat();
targetMovement = new Vector2(message.ReadRangedSingle(-10.0f, 10.0f, 8), message.ReadRangedSingle(-10.0f, 10.0f, 8));
targetMovement.X = MathUtils.Round(targetMovement.X, 0.1f);
targetMovement.Y = MathUtils.Round(targetMovement.Y, 0.1f);
targetMovement = new Vector2 (message.ReadFloat(), message.ReadFloat());
targetDir = message.ReadBoolean();
cursorPos = new Vector2(message.ReadFloat(), message.ReadFloat());
if (aiController==null)
{
cursorPos = new Vector2(
message.ReadFloat(),
message.ReadFloat());
}
}
catch
@@ -1140,8 +1142,8 @@ namespace Subsurface
AnimController.IsStanding = true;
actionKeyDown.State = actionKeyState;
secondaryKeyDown.State = secondaryKeyState;
keys[(int)InputType.ActionHeld].State = actionKeyState;
keys[(int)InputType.SecondaryHeld].State = secondaryKeyState;
if (sendingTime <= LastNetworkUpdate) return;
@@ -1162,11 +1164,11 @@ namespace Subsurface
pos.X = message.ReadFloat();
pos.Y = message.ReadFloat();
vel.X = message.ReadFloat();
vel.Y = message.ReadFloat();
//vel.X = message.ReadFloat();
//vel.Y = message.ReadFloat();
rotation = message.ReadFloat();
angularVel = message.ReadFloat();
//angularVel = message.ReadFloat();
}
catch
{
@@ -1175,10 +1177,10 @@ namespace Subsurface
if (limb.body != null)
{
limb.body.TargetVelocity = vel;
limb.body.TargetVelocity = limb.body.LinearVelocity;
limb.body.TargetPosition = pos;// +vel * (float)(deltaTime / 60.0);
limb.body.TargetRotation = rotation;// +angularVel * (float)(deltaTime / 60.0);
limb.body.TargetAngularVelocity = angularVel;
limb.body.TargetAngularVelocity = limb.body.AngularVelocity;
}
}
@@ -1187,8 +1189,8 @@ namespace Subsurface
try
{
newStunTimer = message.ReadFloat();
newHealth = message.ReadByte();
newStunTimer = message.ReadRangedSingle(0.0f, 60.0f, 8);
newHealth = (message.ReadByte()/255.0f)*maxHealth;
}
catch { return; }
@@ -1196,6 +1198,8 @@ namespace Subsurface
Health = newHealth;
LargeUpdateTimer = 1;
if (aiController != null) aiController.ReadNetworkData(message);
}
else
{
@@ -1210,13 +1214,12 @@ namespace Subsurface
Limb torso = AnimController.GetLimb(LimbType.Torso);
if (torso == null) torso = AnimController.GetLimb(LimbType.Head);
torso.body.TargetPosition = pos;
LargeUpdateTimer = 0;
}
if (aiController != null) aiController.ReadNetworkData(message);
LastNetworkUpdate = sendingTime;
}
@@ -34,7 +34,7 @@ namespace Subsurface
{
get { return pickedItems; }
}
public Sprite HeadSprite
{
get
@@ -156,7 +156,7 @@ namespace Subsurface
break;
}
}
public GUIFrame CreateInfoFrame(Rectangle rect)
{
GUIFrame frame = new GUIFrame(rect, Color.Transparent);
@@ -269,7 +269,7 @@ namespace Subsurface
{
UpdateCharacterItems();
}
if (pickedItems.Count > 0)
{
charElement.Add(new XAttribute("items", string.Join(",", pickedItems)));
@@ -3,6 +3,7 @@ using System.Linq;
using System.Xml.Linq;
using FarseerPhysics;
using Microsoft.Xna.Framework;
using Subsurface.Items.Components;
namespace Subsurface
{
@@ -542,7 +543,7 @@ namespace Subsurface
void UpdateClimbing()
{
if (character.SelectedConstruction == null)
if (character.SelectedConstruction == null || character.SelectedConstruction.GetComponent<Ladder>()==null)
{
Anim = Animation.None;
return;
@@ -623,7 +624,12 @@ namespace Subsurface
torso.body.ApplyForce(climbForce * 40.0f * torso.Mass);
head.body.SmoothRotate(0.0f);
Rectangle trigger = character.SelectedConstruction.Prefab.Triggers.First();
Rectangle trigger = character.SelectedConstruction.Prefab.Triggers.FirstOrDefault();
if (trigger == null)
{
character.SelectedConstruction = null;
return;
}
trigger = character.SelectedConstruction.TransformTrigger(trigger);
//stop climbing if:
@@ -673,10 +679,10 @@ namespace Subsurface
Limb rightHand = GetLimb(LimbType.RightHand);
Limb rightArm = GetLimb(LimbType.RightArm);
Vector2 itemPos = character.SecondaryKeyDown.State ? aimPos : holdPos;
Vector2 itemPos = character.GetInputState(InputType.SecondaryHeld) ? aimPos : holdPos;
float itemAngle;
if (character.SecondaryKeyDown.State && itemPos != Vector2.Zero)
if (character.GetInputState(InputType.SecondaryHeld) && itemPos != Vector2.Zero)
{
Vector2 mousePos = ConvertUnits.ToSimUnits(character.CursorPosition);
+3 -3
View File
@@ -472,7 +472,7 @@ namespace Subsurface
inWater = false;
headInWater = false;
if (currentHull.Volume>currentHull.FullVolume*0.95f || ConvertUnits.ToSimUnits(currentHull.Surface)-floorY> HeadPosition*0.95f)
if (currentHull.Volume > currentHull.FullVolume * 0.95f || ConvertUnits.ToSimUnits(currentHull.Surface) - floorY > HeadPosition * 0.95f)
inWater = true;
}
@@ -562,7 +562,7 @@ namespace Subsurface
private void UpdateNetplayerPosition()
{
Limb refLimb = GetLimb(LimbType.Torso);
if (refLimb== null) refLimb = GetLimb(LimbType.Head);
if (refLimb == null) refLimb = GetLimb(LimbType.Head);
if (refLimb.body.TargetPosition == Vector2.Zero) return;
@@ -603,7 +603,7 @@ namespace Subsurface
if (resetAll)
{
System.Diagnostics.Debug.WriteLine("resetall");
System.Diagnostics.Debug.WriteLine("reset ragdoll limb positions");
foreach (Limb limb in limbs)
{