00001
00009 using System;
00010 using System.Collections.Generic;
00011 using System.Text;
00012
00013 namespace Objects3D
00014 {
00022 public enum ControllerKey
00023 {
00024 KEY_ATTACK,
00025 KEY_DOWN,
00026 KEY_JUMP,
00027 KEY_LEFT,
00028 KEY_RESET,
00029 KEY_RIGHT,
00030 KEY_UP
00031 }
00032
00040 public class Controller
00041 {
00042 protected byte FID = 0;
00043
00044 protected Coordinate FRotation;
00045 protected byte[] FStates;
00046
00047 public Controller()
00048 {
00049 FID = 0;
00050
00051 FRotation = new Coordinate();
00052
00053
00054 FStates = new byte[7]{0, 0, 0, 0, 0, 0, 0};
00055 }
00056
00064 public static int ByteSize
00065 {
00066 get
00067 {
00068 return ((Coordinate.ByteSize * 1) + (sizeof(byte) * 8));
00069 }
00070 }
00071
00083 public bool GetKeyState(ControllerKey AControllerKey)
00084 {
00085 return (FStates[(byte)AControllerKey] != 0);
00086 }
00087
00098 public void SetKeyState(ControllerKey AControllerKey, bool AValue)
00099 {
00100 if ((AControllerKey >= ControllerKey.KEY_ATTACK) && (AControllerKey <= ControllerKey.KEY_UP))
00101 {
00102 if (AValue)
00103 {
00104 FStates[(byte)AControllerKey] = 1;
00105 }
00106 else
00107 {
00108 FStates[(byte)AControllerKey] = 0;
00109 }
00110 }
00111 }
00112
00120 public byte ID
00121 {
00122 get
00123 {
00124 return FID;
00125 }
00126
00127 set
00128 {
00129 FID = value;
00130 }
00131 }
00132
00140 public Coordinate Rotation
00141 {
00142 get
00143 {
00144 return FRotation;
00145 }
00146
00147 set
00148 {
00149 FRotation.Assign(value);
00150 }
00151 }
00152
00162 public byte[] GetBytes()
00163 {
00164 byte[] LResult = new byte[ByteSize];
00165
00166 byte[] LID = new byte[1]{FID};
00167
00168 byte[] LRotation = FRotation.GetBytes();
00169
00170 LID.CopyTo(LResult, (Coordinate.ByteSize * 0) + (sizeof(byte) * 0));
00171 LRotation.CopyTo(LResult, (Coordinate.ByteSize * 0) + (sizeof(byte) * 1));
00172 FStates.CopyTo(LResult, (Coordinate.ByteSize * 1) + (sizeof(byte) * 1));
00173
00174 return LResult;
00175 }
00176
00189 public bool SetBytes(byte[] ABytes, int AOffset)
00190 {
00191 bool LResult = false;
00192
00193 if (((ABytes.Length - AOffset) >= ByteSize) && (AOffset >= 0))
00194 {
00195 FID = ABytes[AOffset++];
00196
00197 FRotation.SetBytes(ABytes, AOffset);
00198 AOffset += Coordinate.ByteSize;
00199
00200 for (int LIndex = AOffset; LIndex < ABytes.Length; LIndex++)
00201 {
00202 FStates[LIndex - AOffset] = ABytes[LIndex];
00203 }
00204
00205 LResult = true;
00206 }
00207
00208 return LResult;
00209 }
00210
00222 public bool Assign(Controller AController)
00223 {
00224 bool LResult = false;
00225
00226 if (AController != null)
00227 {
00228 ID = AController.ID;
00229
00230 SetKeyState(ControllerKey.KEY_ATTACK, AController.GetKeyState(ControllerKey.KEY_ATTACK));
00231 SetKeyState(ControllerKey.KEY_DOWN, AController.GetKeyState(ControllerKey.KEY_DOWN));
00232 SetKeyState(ControllerKey.KEY_JUMP, AController.GetKeyState(ControllerKey.KEY_JUMP));
00233 SetKeyState(ControllerKey.KEY_LEFT, AController.GetKeyState(ControllerKey.KEY_LEFT));
00234 SetKeyState(ControllerKey.KEY_RESET, AController.GetKeyState(ControllerKey.KEY_RESET));
00235 SetKeyState(ControllerKey.KEY_RIGHT, AController.GetKeyState(ControllerKey.KEY_RIGHT));
00236 SetKeyState(ControllerKey.KEY_UP, AController.GetKeyState(ControllerKey.KEY_UP));
00237
00238 LResult = FRotation.Assign(AController.Rotation);
00239 }
00240
00241 return LResult;
00242 }
00243 }
00244 }