diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 00000000..5b022970 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Michael Smith + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/assets/game_projects/fighter/src/game_state.py b/assets/game_projects/fighter/src/fight_simulator.py similarity index 88% rename from assets/game_projects/fighter/src/game_state.py rename to assets/game_projects/fighter/src/fight_simulator.py index 269d8904..a97174b1 100644 --- a/assets/game_projects/fighter/src/game_state.py +++ b/assets/game_projects/fighter/src/fight_simulator.py @@ -3,16 +3,11 @@ from roll.node import Node from roll.math import Vector2 +from assets.game_projects.fighter.src.fight_state import PlayerStateData from assets.game_projects.fighter.src.input_buffer import InputBuffer -class PlayerStateData: - def __init__(self, player_node: Node, player_input_buffer: InputBuffer): - self.player_node = player_node - self.player_input_buffer = player_input_buffer - - -class GameState: +class FightSimulator: def simulate_frame( self, frame: int, diff --git a/assets/game_projects/fighter/src/fight_state.py b/assets/game_projects/fighter/src/fight_state.py new file mode 100644 index 00000000..21b16f79 --- /dev/null +++ b/assets/game_projects/fighter/src/fight_state.py @@ -0,0 +1,75 @@ +from roll.node import Node +from roll.math import Vector2 + +from assets.game_projects.fighter.src.input_buffer import InputBuffer + + +class PlayerStateData: + def __init__(self, player_node: Node, player_input_buffer: InputBuffer): + self.player_node = player_node + self.player_input_buffer = player_input_buffer + + +class PlayerFrameState: + def __init__(self, name: str, position: Vector2, inputs: list): + self.name = name + self.position = position + self.inputs = inputs + + def __str__(self): + return f"name: '{self.name}', position = '{self.position}', inputs = '{self.inputs}'" + + def __repr__(self): + return f"name: '{self.name}', position = '{self.position}', inputs = '{self.inputs}'" + + +class FrameState: + def __init__( + self, + frame: int, + player_one_state_data: PlayerStateData, + player_two_state_data: PlayerStateData, + ): + self.frame = frame + self.player_one_frame_state = None + self.player_two_frame_state = None + + if player_one_state_data: + self.player_one_frame_state = PlayerFrameState( + name="PlayerOne", + position=player_one_state_data.player_node.position, + inputs=player_one_state_data.player_input_buffer.values, + ) + if player_two_state_data: + self.player_two_frame_state = PlayerFrameState( + name="PlayerTwo", + position=player_two_state_data.player_node.position, + inputs=player_two_state_data.player_input_buffer.values, + ) + + def __str__(self): + return f"frame: '{self.frame}', player_one_state = '{self.player_one_frame_state}', player_two_state = '{self.player_two_frame_state}'" + + def __repr__(self): + return f"frame: '{self.frame}', player_one_state = '{self.player_one_frame_state}', player_two_state = '{self.player_two_frame_state}'" + + +class FightState: + def __init__(self): + self.frame_states = {} + + def serialize( + self, + frame: int, + player_one_state_data: PlayerStateData, + player_two_state_data: PlayerStateData, + ) -> None: + self.frame_states[frame] = FrameState( + frame, player_one_state_data, player_two_state_data + ) + + def __str__(self): + return f"{self.frame_states}" + + def __repr__(self): + return f"{self.frame_states}" diff --git a/assets/game_projects/fighter/src/main.py b/assets/game_projects/fighter/src/main.py index c8b6845b..c528ecaa 100644 --- a/assets/game_projects/fighter/src/main.py +++ b/assets/game_projects/fighter/src/main.py @@ -6,12 +6,13 @@ from roll.math import Vector2 from roll.network import Server, Client, Network +from assets.game_projects.fighter.src.fight_state import FightState, PlayerStateData from assets.game_projects.fighter.src.input_buffer import ( InputBuffer, OutgoingNetworkInputBuffer, IncomingNetworkInputBuffer, ) -from assets.game_projects.fighter.src.game_state import GameState, PlayerStateData +from assets.game_projects.fighter.src.fight_simulator import FightSimulator from assets.game_projects.fighter.src.game_properties import ( GameProperties, PropertyValue, @@ -24,8 +25,9 @@ class Main(Node2D): def _start(self) -> None: - self.game_state = GameState() + self.fight_simulator = FightSimulator() self.game_properties = GameProperties() + self.fight_state = FightState() self.frame_counter = 0 self.input_buffers = [] @@ -144,6 +146,13 @@ def _physics_process(self, delta_time: float) -> None: self._process_simulation() + self.fight_state.serialize( + frame=self.frame_counter, + player_one_state_data=self.player_one_state_data, + player_two_state_data=self.player_two_state_data, + ) + # print(f"fight_state = {self.fight_state}") + self.game_properties.has_received_network_inputs = False self.frame_counter += 1 @@ -155,7 +164,7 @@ def _process_inputs(self) -> None: input_buffer.poll_client_inputs(frame=self.frame_counter) def _process_simulation(self) -> None: - self.game_state.simulate_frame( + self.fight_simulator.simulate_frame( frame=self.frame_counter, player_one_state_data=self.player_one_state_data, player_two_state_data=self.player_two_state_data,