Skip to content

Commit

Permalink
Updated name for Fight Simulator (from GameState). Serializing game s…
Browse files Browse the repository at this point in the history
…tate each frame. Added license.
  • Loading branch information
Chukobyte committed May 15, 2021
1 parent c970eaa commit 3370e6e
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 10 deletions.
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
75 changes: 75 additions & 0 deletions assets/game_projects/fighter/src/fight_state.py
Original file line number Diff line number Diff line change
@@ -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}"
15 changes: 12 additions & 3 deletions assets/game_projects/fighter/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 = []

Expand Down Expand Up @@ -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

Expand All @@ -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,
Expand Down

0 comments on commit 3370e6e

Please sign in to comment.