Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- // Need this for playerposition info
- using BattleCore.Events;
- namespace Drone
- {
- class Rotation
- {
- private byte _DefaultRotation;
- private ushort _DroneX;
- private ushort _DroneY;
- /// <summary>
- /// Set drone position
- /// </summary>
- /// <param name="x">x coord</param>
- /// <param name="y">y coord</param>
- /// <param name="rotation">rotation drone returns to when there are no targets.</param>
- public void SetVariables(ushort x, ushort y, byte rotation)
- {
- _DroneX = x;
- _DroneY = y;
- _DefaultRotation = rotation;
- }
- /// <summary>
- /// This will increment 1 rotation toward the target.
- /// </summary>
- /// <param name="position">Player position packet.</param>
- /// <param name="NewRotation">Rotation to target.</param>
- public void UpdatedRotation(PlayerPositionEvent position, byte NewRotation)
- {
- if (position.ShipRotation != NewRotation)
- {
- // Special circumstance if ActR == 19
- if ((position.ShipRotation == 39) && (NewRotation <= 19))
- position.ShipRotation = 0;
- // Special circumstance if ActR == 0
- else if (position.ShipRotation == 0)
- {
- if (NewRotation <= 20)
- position.ShipRotation++;
- else
- position.ShipRotation = 39;
- }
- else if (NewRotation > position.ShipRotation)
- {
- if (NewRotation - position.ShipRotation < 20)
- position.ShipRotation++;
- else
- position.ShipRotation--;
- }
- else
- {
- if (position.ShipRotation - NewRotation < 20)
- position.ShipRotation--;
- else
- position.ShipRotation++;
- }
- }
- }
- /// <summary>
- /// If no target is found in the area have drone return to default rotation.
- /// </summary>
- /// <param name="position">Player position packet.</param>
- public void NoTarget(PlayerPositionEvent position)
- {
- UpdatedRotation(position, _DefaultRotation);
- }
- public byte RotationToTarget(ushort PlayerPosX, ushort PlayerPosY)
- {
- //setting info to variables (i can take this out later)
- ushort play_x = PlayerPosX;
- ushort play_y = PlayerPosY;
- ushort bot_x = _DroneX;
- ushort bot_y = _DroneY;
- // Formula to extract an angle
- double angle = Math.Atan((double)(play_y - bot_y) / (double)(play_x - bot_x)) * (180.0 / 3.14159);
- byte rot = 0;
- // with formula we get 0-90 degrees for each quadrant
- // here we simply find out which quadrant it is and add the proper increment to make it rotation 0-39
- if (play_x > bot_x)
- {
- if (play_y > bot_y)// Lower Right
- {
- rot = (byte)Math.Floor((angle / 9) + 10);
- }
- else if (play_y < bot_y)// Top Right
- {
- rot = (byte)Math.Floor((90 + angle) / 9);
- }
- }
- else if (play_x < bot_x)
- {
- if (play_y > bot_y)// Lower Left
- {
- rot = (byte)Math.Floor(((90 + angle) / 9) + 19);
- }
- else if (play_y < bot_y)// Top Left
- {
- rot = (byte)Math.Floor((angle / 9) + 29);
- }
- }
- return rot;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment