-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add basics for mouse input. * basic class for MouseInput
- Loading branch information
Showing
1 changed file
with
60 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |