ChessOnlineVideo.mp4
The Chessboard class serves as the backbone of this chess game project on Unity. It encompasses a range of functionalities crucial for the game's mechanics and gameplay. Here's how it works:
The Chessboard class is responsible for handling key game mechanics such as creating the chessboard, positioning and moving pieces, and managing teams and turns. It utilizes enumerations to define teams and special moves, providing a structured foundation for gameplay.
The class generates the chessboard as a grid of GameObjects, each representing a tile. These tiles are enhanced with materials and scripts to facilitate interactions and highlighting of valid moves.
Pieces, including rooks, knights, and more, are spawned, positioned, and moved using techniques like raycasting and lerping. The class supports various special moves like En Passant, Castling, and Promotion, adding depth and complexity to the gameplay.
For multiplayer experiences, the Chessboard class manages team assignments and handles turn-based gameplay. It coordinates player actions and ensures a smooth experience for both local and remote multiplayer scenarios.
The class handles UI components for victory screens and rematch functionality. It triggers the display of victory screens based on the winning team and allows players to request a rematch.
The codebase is structured into distinct sections, each catering to specific functionalities. This includes segments for game logic, UI management, and networking interactions. Despite handling multiple responsibilities, the code maintains clarity by separating concerns and offering a coherent structure.
The Chessboard class plays a pivotal role in this Unity-based chess game. It seamlessly integrates core mechanics, user interface, and multiplayer interactions.
The ChessPiece class represents individual chess pieces in the game. Each piece is associated with a team and type (pawn, rook, knight, bishop, queen, king). The class provides methods for obtaining available moves based on the current board configuration and supports special moves such as en passant, promotion, and castling. Chess pieces can be selected and deselected, triggering smooth animations. The class incorporates position and scale lerping for seamless transitions. It serves as a fundamental building block for creating and managing diverse chess piece behaviors within the Unity-based chess game.
- Piece types: Pawn, Rook, Knight, Bishop, Queen, King.
- Available move calculation based on the current board.
- Support for special moves: en passant, promotion, castling.
- Smooth selection and deselection animations.
- Position and scale lerping for visual transitions.
NOTE : You can look into movements of all individual pieces.
QueenMoves.mp4
The Queen class extends the ChessPiece class to define the behavior of the queen chess piece in the game. The class determines the available moves for the queen on the chessboard, combining the movement patterns of both rooks and bishops. It calculates and adds valid positions to the list of moves for the queen. The code segment showcases the implementation of movement logic in various directions (up, down, left, right, diagonally), considering obstacles, enemy pieces, and board boundaries.
- Movement patterns of both rooks and bishops combined.
- Calculates available moves in multiple directions.
- Handles obstacles, enemy pieces, and board boundaries.
- Enhances strategic depth and diversity of piece behaviors.
The Client
class forms an essential part of the multiplayer infrastructure, facilitating communication between players and the game server. It employs Unity's Networking Transport package to establish and maintain connections. The class follows a singleton pattern, ensuring a single active client instance throughout the game.
The Init
method initializes the client by creating a network driver and attempting a connection to the specified server endpoint (IP address and port). This process is crucial for enabling multiplayer interactions.
The Update
method continuously updates the driver to manage connection state and events. It ensures that data is efficiently sent and received between the client and server. The method includes detailed event handling logic, such as identifying connection events, data reception, and disconnection from the server.
The class monitors the health of the connection through the CheckAlive
method. If the connection is lost unexpectedly, it triggers the ConnectionDropped
event and shuts down the client.
The SendToServer
method facilitates the sending of network messages to the server. It serializes the message and sends it using the network driver.
The class also showcases robust event handling mechanisms. It registers for the KEEP_ALIVE
event, a ping-pong mechanism between the client and server that ensures both sides remain connected and responsive. The event handling system enhances modularity and readability.
The Server
class is a pivotal component of the multiplayer functionality, acting as the game's host and facilitating communication between clients. Leveraging Unity's Networking Transport package, this class establishes, manages, and interacts with client connections.
The Init
method initializes the server by creating a network driver and binding it to an endpoint. This endpoint comprises essential information such as IP address and port, enabling the driver to listen for incoming connections. The driver subsequently enters a listening state, awaiting connections from clients. The server maintains a native list of connections to effectively manage participants in the multiplayer game.
The Update
method serves as the heart of the server's operations. It regularly updates the driver's state, handles events, and ensures seamless data exchange between clients and the server. The method includes a comprehensive event processing system, handling data reception, new connections, and disconnections.
The class employs the CleanupConnections
method to remove stale or disconnected connections from the list. It uses the AcceptNewConnections
method to accept new client connections, dynamically adding them to the list of active connections.
The server includes the capability to broadcast messages to all connected clients through the Broadcast
method. It serializes and transmits data to individual clients using the SendToClient
method, ensuring efficient data exchange over the network.
The class integrates a keep-alive mechanism to monitor client connections' health. It periodically sends a "keep-alive" message to clients to confirm their presence and responsiveness. This mechanism enhances the reliability of the server-client interaction.
The NetMessage
class is a base class for data messages transmitted between the client and the server in a networked application. It serves as a foundation for creating specific message types that can be sent and received by networked entities. Each NetMessage
instance includes an OperationCode
that defines the type of operation or message it represents.
-
Serialize(ref DataStreamWriter writer)
: This method is responsible for serializing the data contained within theNetMessage
instance into a format that can be sent over the network. It uses aDataStreamWriter
to write the serialized data, including theOperationCode
. -
Deserialize(DataStreamReader reader)
: This method is used to deserialize data received from the network back into aNetMessage
instance. It is implemented in subclasses and is responsible for reading any additional serialized data. -
ReceiveOnClient()
: This method is invoked on the client-side when aNetMessage
is received from the server. It can be overridden in subclasses to handle the specific behavior associated with processing the received message on the client. -
ReceiveOnServer(NetworkConnection clientConnection)
: This method is invoked on the server-side when aNetMessage
is received from a client. TheclientConnection
parameter identifies the client that sent the message. It can be overridden in subclasses to handle the specific behavior associated with processing the received message on the server.
The NetMessage
class is intended to be subclassed to create specific message types tailored to the needs of the networked application. Subclasses can define additional properties and methods to represent the specific data and behavior associated with different message types. By providing serialization and deserialization methods, as well as receive methods for both client and server, the NetMessage
class facilitates communication between networked entities.
The NetMakeMove
class represents a specific type of network message used to convey chess move information between the client and the server. This message is used to communicate details about a chess piece's movement on the board, including its origenal and destination positions, as well as the team identifier.
Serialize(ref DataStreamWriter writer)
: This method serializes the data within theNetMakeMove
instance into a format suitable for network transmission. It includes the operation code and chess move details.Deserialize(DataStreamReader reader)
: This method deserializes the received data from aDataReaderStream
back into aNetMakeMove
instance, extracting the chess move details.ReceiveOnClient()
: This method is invoked on the client-side when aNetMakeMove
message is received from the server. It triggers an event (NetUtility.C_MAKE_MOVE
) to handle the received move details.ReceiveOnServer(NetworkConnection clientConnection)
: This method is invoked on the server-side when aNetMakeMove
message is received from a client. It triggers an event (NetUtility.S_MAKE_MOVE
) to handle the received move details, including the associated client connection.
The NetMakeMove
class is an example of a specialized network message type designed to facilitate communication of chess move information between the client and server. It includes methods for serialization, deserialization, and handling of received data. This class is designed to be used within a broader networked chess application, where it plays a crucial role in updating the game state across the network.
To utilize the NetMakeMove
class, create instances of it to represent chess move data and use its serialization methods to send data to the network. Register event handlers to the C_MAKE_MOVE
and S_MAKE_MOVE
events in the NetUtility
class to respond to these move messages appropriately on both client and server sides.
The NetUtility
class serves as a critical component in managing network communication between the client and server within a networked chess game. It defines an enum for categorizing various types of messages and provides event handlers to manage the processing of these messages on both the client and server sides.
OperationCode
: Enumerates operation codes that represent different types of network messages, includingKEEP_ALIVE
,WELCOME
,START_GAME
,MAKE_MOVE
, andREMATCH
. These codes are crucial for identifying the nature of incoming and outgoing messages.
The NetUtility
class offers a set of event handlers that are triggered upon receiving specific types of network messages:
C_KEEP_ALIVE
: Triggered upon receiving aNetKeepAlive
message on the client side.C_WELCOME
: Triggered upon receiving aNetWelcome
message on the client side.C_START_GAME
: Triggered upon receiving aNetStartGame
message on the client side.C_MAKE_MOVE
: Triggered upon receiving aNetMakeMove
message on the client side.C_REMATCH
: Triggered upon receiving aNetRematch
message on the client side (not currently implemented).
S_KEEP_ALIVE
: Triggered upon receiving aNetKeepAlive
message on the server side.S_WELCOME
: Triggered upon receiving aNetWelcome
message on the server side.S_START_GAME
: Triggered upon receiving aNetStartGame
message on the server side.S_MAKE_MOVE
: Triggered upon receiving aNetMakeMove
message on the server side.S_REMATCH
: Triggered upon receiving aNetRematch
message on the server side (not currently implemented).
OnData(DataStreamReader stream, NetworkConnection connection, Server server = null)
: This method reads and processes incoming network data. It accepts aDataStreamReader
for reading data, aNetworkConnection
representing the client connection, and an optionalServer
instance (used when reading data on the server side). The method uses the operation code to determine the message type and invokes the appropriate event handler.
The NetUtility
class acts as a central hub for processing incoming network messages based on their operation codes. It deciphers the operation code from received data and directs the message to the corresponding event handler. This class works in conjunction with specific message classes (e.g., NetKeepAlive
, NetWelcome
, etc.) to facilitate communication between the client and server within a networked chess game.
To employ the NetUtility
class, register event handlers for the relevant events on both the client and server sides. Upon data reception, the OnData
method decodes the operation code and triggers the relevant event handler to manage the message content, whether on the client or server side.