Skip to content

Commit

Permalink
Satchel f mouse input (#28)
Browse files Browse the repository at this point in the history
* add basics for mouse input.

* basic class for MouseInput
  • Loading branch information
SatchelF authored Apr 2, 2024
1 parent 2bb172e commit d8906c7
Showing 1 changed file with 60 additions and 5 deletions.
65 changes: 60 additions & 5 deletions src/Client/Systems/MouseInput.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,65 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Shared.Entities;
using System;
using System.Collections.Generic;
using Shared.Components;

namespace Client.Systems;

public class MouseInput : Shared.Systems.System
namespace Client.Systems
{
public override void update(TimeSpan elapsedTime)
public class MouseInput : Shared.Systems.System
{
private MouseState previousMouseState = Mouse.GetState();
private Controls m_controls;

public MouseInput(Controls controls) : base(typeof(Shared.Components.Input))
{
m_controls = controls;
}

public override void update(TimeSpan elapsedTime)
{
var mouseState = Mouse.GetState();
var currentPosition = new Vector2(mouseState.X, mouseState.Y);

foreach (var entityPair in m_entities)
{
var entity = entityPair.Value;
// Try to get the Position component safely
try
{
var positionComponent = entity.get<Position>();

var wormHeadPosition = positionComponent.position;
var direction = currentPosition - wormHeadPosition;

// Check mouse movement relative to the worm head's position to decide on turn direction
if (direction.X < 0 && currentPosition != previousMouseState.Position.ToVector2()) // Mouse moved left
{
HandleTurnLeft(entity, elapsedTime);
}
else if (direction.X > 0 && currentPosition != previousMouseState.Position.ToVector2()) // Mouse moved right
{
HandleTurnRight(entity, elapsedTime);
}
}
catch (KeyNotFoundException)
{
// Component not found, skip this entity
}
}

previousMouseState = mouseState;
}

private void HandleTurnLeft(Entity entity, TimeSpan elapsedTime)
{
// Logic for left turn
}

private void HandleTurnRight(Entity entity, TimeSpan elapsedTime)
{
// Logic for right turn
}
}
}
}

0 comments on commit d8906c7

Please sign in to comment.