287 lines
7.6 KiB
C#
287 lines
7.6 KiB
C#
using System.Collections.Generic;
|
|
using System.ComponentModel.Composition;
|
|
using System.Linq;
|
|
using System.Net.Http.Headers;
|
|
using UnityEngine;
|
|
|
|
public class TargetCursor : MonoBehaviour
|
|
{
|
|
public GameManager gameManager;
|
|
public GameObject cursorVisual;
|
|
|
|
private bool isShootingInProgress = false;
|
|
public bool isPlacingShips = false;
|
|
public GamePole ownField;
|
|
|
|
private int x = 4;
|
|
private int y = 4;
|
|
private int width = 10;
|
|
private int height = 10;
|
|
|
|
private Direction currentDirection = Direction.Right;
|
|
|
|
private PlayerKeySet player1Keys = new PlayerKeySet
|
|
{
|
|
Up = ButtonPinMapping.up1,
|
|
Down = ButtonPinMapping.down1,
|
|
Left = ButtonPinMapping.left1,
|
|
Right = ButtonPinMapping.right1,
|
|
Fire = ButtonPinMapping.action1,
|
|
Turn = ButtonPinMapping.turn1
|
|
};
|
|
|
|
private PlayerKeySet player2Keys = new PlayerKeySet
|
|
{
|
|
Up = ButtonPinMapping.up2,
|
|
Down = ButtonPinMapping.down2,
|
|
Left = ButtonPinMapping.left2,
|
|
Right = ButtonPinMapping.right2,
|
|
Fire = ButtonPinMapping.action2,
|
|
Turn = ButtonPinMapping.turn2
|
|
};
|
|
|
|
private PlayerKeySet activeKeys;
|
|
|
|
|
|
// êíîïî÷êè
|
|
private ButtonPinMapping upKey;
|
|
private ButtonPinMapping downKey;
|
|
private ButtonPinMapping leftKey;
|
|
private ButtonPinMapping rightKey;
|
|
private ButtonPinMapping fireKey;
|
|
private ButtonPinMapping turnKey;
|
|
|
|
|
|
public Transform cameraPivot;
|
|
public float rotateSpeed = 90f;
|
|
|
|
private int playerId;
|
|
private int enemyId;
|
|
|
|
public void Initialize(GameManager gm, int playerId = 1, bool isPlacingShips = false)
|
|
{
|
|
this.isPlacingShips = isPlacingShips;
|
|
gameManager = gm;
|
|
|
|
if (isPlacingShips)
|
|
ownField = gm.playerField;
|
|
else
|
|
ownField = gm.enemyField;
|
|
|
|
BindKeys(playerId);
|
|
|
|
if (cameraPivot == null)
|
|
{
|
|
cameraPivot = gameManager.cameraPivot;
|
|
gameManager.playerCamera.transform.SetParent(cameraPivot);
|
|
}
|
|
|
|
// âñåãäà íàñòðàèâàåì êîíòðîëëåð
|
|
var camCtrl = cameraPivot.GetComponent<CameraPivotController>();
|
|
if (camCtrl == null) camCtrl = cameraPivot.gameObject.AddComponent<CameraPivotController>();
|
|
camCtrl.Setup(
|
|
gameManager,
|
|
cameraPivot,
|
|
leftKey, // òå æå êíîïêè
|
|
rightKey,
|
|
turnKey,
|
|
rotateSpeed,
|
|
this // àðãóìåíò åñòü â ñèãíàòóðå, íî âíóòðè íå èñïîëüçóåòñÿ
|
|
|
|
);
|
|
|
|
enemyId = gameManager.opponentManager.playerId;
|
|
|
|
this.playerId = playerId;
|
|
|
|
Debug.Log("Ðîäèëñÿ");
|
|
|
|
|
|
UpdateCursorPosition();
|
|
}
|
|
|
|
private void BindKeys(int playerId)
|
|
{
|
|
activeKeys = (playerId == 1) ? player1Keys : player2Keys;
|
|
|
|
upKey = activeKeys.Up;
|
|
downKey = activeKeys.Down;
|
|
leftKey = activeKeys.Left;
|
|
rightKey = activeKeys.Right;
|
|
fireKey = activeKeys.Fire;
|
|
turnKey = activeKeys.Turn;
|
|
}
|
|
|
|
|
|
public void SwitchButton(Vector3 rotate)
|
|
{
|
|
float y = rotate.y;
|
|
if (y > 180) y -= 360;
|
|
|
|
Debug.Log($"Y rotation = {y}");
|
|
|
|
// Âïåð¸ä
|
|
if (y >= -45 && y < 45)
|
|
{
|
|
upKey = activeKeys.Up;
|
|
leftKey = activeKeys.Left;
|
|
downKey = activeKeys.Down;
|
|
rightKey = activeKeys.Right;
|
|
}
|
|
// Âëåâî
|
|
else if (y >= -135 && y < -45)
|
|
{
|
|
upKey = activeKeys.Right;
|
|
leftKey = activeKeys.Up;
|
|
downKey = activeKeys.Left;
|
|
rightKey = activeKeys.Down;
|
|
}
|
|
// Âïðàâî
|
|
else if (y >= 45 && y < 135)
|
|
{
|
|
upKey = activeKeys.Left;
|
|
leftKey = activeKeys.Down;
|
|
downKey = activeKeys.Right;
|
|
rightKey = activeKeys.Up;
|
|
}
|
|
// Íàçàä (180°)
|
|
else
|
|
{
|
|
upKey = activeKeys.Down;
|
|
leftKey = activeKeys.Right;
|
|
downKey = activeKeys.Up;
|
|
rightKey = activeKeys.Left;
|
|
}
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (isShootingInProgress) return;
|
|
if (gameManager.currentInputMode == GameManager.InputMode.UI) return;
|
|
|
|
if (In.GetButtonDown(turnKey) && (In.GetButtonDown(leftKey) || In.GetButtonDown(rightKey)))
|
|
return;
|
|
|
|
// --- Ïîâîðîò êîðàáëÿ â ðåæèìå ðàññòàíîâêè ---
|
|
if (isPlacingShips && In.GetButtonDown(turnKey))
|
|
{
|
|
RotateDirection();
|
|
UpdateCursorPosition();
|
|
}
|
|
|
|
// --- Òîëüêî â ñâîé õîä ---
|
|
if (!isPlacingShips && (gameManager.currentState != GameState.Playing || !gameManager.isPlayerTurn))
|
|
return;
|
|
|
|
// --- Äâèæåíèå êóðñîðà ---
|
|
bool moved = false;
|
|
if (In.GetButtonDown(upKey)) { MoveCursor(0, -1); moved = true; }
|
|
if (In.GetButtonDown(downKey)) { MoveCursor(0, 1); moved = true; }
|
|
if (In.GetButtonDown(rightKey)) { MoveCursor(1, 0); moved = true; }
|
|
if (In.GetButtonDown(leftKey)) { MoveCursor(-1, 0); moved = true; }
|
|
if (moved) UpdateCursorPosition();
|
|
|
|
// --- Âûñòðåë / óñòàíîâêà ---
|
|
if (In.GetButtonDown(fireKey))
|
|
{
|
|
if (!isPlacingShips) TryShoot();
|
|
else TryPlaceCurrentShip();
|
|
}
|
|
}
|
|
|
|
void UpdateCursorPosition()
|
|
{
|
|
Cell targetCell = ownField.GetCell(x, y);
|
|
if (targetCell != null)
|
|
{
|
|
cursorVisual.transform.position = targetCell.transform.position + Vector3.back * 0.1f;
|
|
}
|
|
if (isPlacingShips)
|
|
{
|
|
ownField.ClearGhost();
|
|
ownField.ShowShipGhost(x, y, currentDirection);
|
|
}
|
|
}
|
|
|
|
void TryShoot()
|
|
{
|
|
if (isShootingInProgress) return;
|
|
if (ownField.PoleShot[x, y] != 0) return;
|
|
|
|
|
|
isShootingInProgress = true;
|
|
|
|
if (gameManager.gameMode == GameMode.PlayerVsPlayer)
|
|
{
|
|
gameManager.opponentManager.enemyBattery.FireShot(
|
|
gameManager.opponentManager.playerField, x, y, (wasHit, wasShipDestroyed) => { // ìèñòð êàñòûëü
|
|
|
|
gameManager.opponentManager.ProcessShotResult(wasHit, false);
|
|
});
|
|
}
|
|
|
|
gameManager.battery.FireShot(gameManager.enemyField, x, y, (wasHit, wasShipDestroyed) =>
|
|
{
|
|
gameManager.ProcessShotResult(wasHit, true);
|
|
if (!wasHit)gameManager.EndPlayerTurn();
|
|
isShootingInProgress = false;
|
|
});
|
|
|
|
}
|
|
|
|
void TryPlaceCurrentShip()
|
|
{
|
|
bool placed = ownField.PlaceShipManual(x, y, currentDirection);
|
|
if (placed) gameManager.AddShipPlayer();
|
|
}
|
|
|
|
void RotateDirection()
|
|
{
|
|
currentDirection = (Direction)(((int)currentDirection + 1) % 4);
|
|
}
|
|
|
|
void MoveCursor(int dx, int dy)
|
|
{
|
|
if (isPlacingShips)
|
|
{
|
|
x += dx;
|
|
y += dy;
|
|
|
|
if (x < 0) x = width - 1;
|
|
if (x >= width)
|
|
{
|
|
x = width - 1;
|
|
gameManager.SetInputMode(GameManager.InputMode.UI);
|
|
return;
|
|
}
|
|
|
|
if (y < 0) y = height - 1;
|
|
if (y >= height) y = 0;
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
x += dx;
|
|
y += dy;
|
|
|
|
if (x < 0) x = width - 1;
|
|
if (x >= width) x = 0;
|
|
if (y < 0) y = height - 1;
|
|
if (y >= height) y = 0;
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
[System.Serializable]
|
|
public class PlayerKeySet
|
|
{
|
|
public ButtonPinMapping Up;
|
|
public ButtonPinMapping Down;
|
|
public ButtonPinMapping Left;
|
|
public ButtonPinMapping Right;
|
|
public ButtonPinMapping Fire;
|
|
public ButtonPinMapping Turn;
|
|
}
|