-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
256 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2024 Alex0vSky (https://github.com/Alex0vSky), Copyright 2015-2021 (https://github.com/KrystianKaluzny/Tanks) | ||
#include "net.h" | ||
|
||
namespace net { | ||
void NetGame::update(Uint32 dt) { | ||
// TODO(alex): get from network | ||
constexpr auto MODE = cista::mode::NONE | ||
| cista::mode::WITH_VERSION | ||
| cista::mode::WITH_INTEGRITY | ||
| cista::mode::DEEP_CHECK | ||
; | ||
if ( m_player ) { | ||
std::vector< unsigned char > buffer; | ||
NetPlayer *player; | ||
buffer = cista::serialize< MODE >( *m_player ); | ||
player = cista::deserialize< NetPlayer, MODE >( buffer ); | ||
*m_player = *player; | ||
__nop( ); | ||
} | ||
// Initial rewrite | ||
if ( !m_player ) { | ||
for ( auto player : Game::m_players ) | ||
delete player; | ||
Game::m_players.clear( ); | ||
|
||
unsigned playerIndex = 0; | ||
m_player = std::make_shared< NetPlayer > ( | ||
AppConfig::player_starting_point.at( playerIndex ).x | ||
, AppConfig::player_starting_point.at( playerIndex ).y | ||
, sprite_t::ST_PLAYER_1 | ||
); | ||
m_player ->player_keys = AppConfig::player_keys.at( playerIndex ); | ||
++playerIndex; | ||
Game::m_players.push_back( m_player.get( ) ); | ||
} | ||
|
||
Game::update( dt ); | ||
} | ||
} // namespace net | ||
|
||
using hash_t = cista::hash_t; | ||
hash_t type_hash(SpriteDataWrapper const& el, hash_t h, | ||
std::map<hash_t, unsigned>& done) noexcept { | ||
return cista::hash_combine( h, cista::hash("SpriteDataWrapper") ); | ||
} | ||
|
||
template <typename Ctx> | ||
inline void serialize(Ctx & context, SpriteDataWrapper const* el, cista::offset_t const offset) { | ||
using cista::serialize; | ||
serialize( context, &el ->m_sprite, offset + offsetof( SpriteDataWrapper, m_sprite ) ); | ||
serialize( context, &el ->m_spriteType, offset + offsetof( SpriteDataWrapper, m_spriteType ) ); | ||
} | ||
|
||
template <typename Ctx> | ||
void deserialize(Ctx const& c, net::NetPlayer* el) { | ||
const auto &sc = Engine::getEngine().getSpriteConfig(); | ||
sprite_t spriteType; | ||
el ->m_sprite = sc ->getSpriteData( el ->m_sprite.getType( ) ); | ||
spriteType = el ->m_shield.m_sprite.getType( ); | ||
const SpriteData* spriteData = nullptr; | ||
if ( sprite_t::ST_NONE != spriteType ) | ||
spriteData = sc ->getSpriteData( spriteType ); | ||
el ->m_shield.m_sprite = spriteData; | ||
|
||
for ( auto &bullet : el ->bullets ) | ||
bullet.m_sprite = sc ->getSpriteData( bullet.m_sprite.getType( ) ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#pragma once // Copyright 2024 Alex0vSky (https://github.com/Alex0vSky), Copyright 2015-2021 (https://github.com/KrystianKaluzny/Tanks) | ||
#include "iappstate.h" | ||
#include "appconfig.h" | ||
#include "game.h" | ||
|
||
namespace net { | ||
|
||
class NetPlayer : public Player { | ||
using Player::Player; | ||
|
||
public: | ||
void update(Uint32 dt) override { | ||
Tank::update( dt ); | ||
// Cut keyboard and audio processing | ||
std::vector< Uint8 > keys; | ||
{ | ||
int numkeys = 0; | ||
const Uint8 *key_state = ::SDL_GetKeyboardState( &numkeys ); | ||
keys.resize( numkeys ); | ||
if ( key_state != nullptr ) | ||
memcpy( &keys[ 0 ], key_state, numkeys * sizeof( Uint8 ) ); | ||
//SDL_ResetKeyboard( ); | ||
} | ||
if ( keys.size( ) ) { | ||
m_movement = false; | ||
Direction direction_ = Direction::D_INITIAL; | ||
if ( keys[ player_keys.up ] ) | ||
direction_ = Direction::D_UP; | ||
else if ( keys[ player_keys.down ] ) | ||
direction_ = Direction::D_DOWN; | ||
else if ( keys[ player_keys.left ] ) | ||
direction_ = Direction::D_LEFT; | ||
else if ( keys[ player_keys.right ] ) | ||
direction_ = Direction::D_RIGHT; | ||
else if ( !testFlag( TankStateFlag::TSF_ON_ICE ) || m_slip_time == 0 ) | ||
speed = 0.0; | ||
if ( Direction::D_INITIAL != direction_ ) | ||
setDirection( direction_ ), speed = default_speed, m_movement = true; | ||
// TODO(alex): `Engine::getEngine( ).getAudio( ) ->playSound( ) ...` | ||
|
||
if ( keys[ player_keys.fire ] && m_fire_time > AppConfig::player_reload_time ) | ||
fire( ), m_fire_time = 0; | ||
} | ||
m_fire_time += dt; | ||
if(testFlag(TankStateFlag::TSF_LIFE)) | ||
src_rect = moveRect( m_sprite->rect | ||
, ( | ||
testFlag(TankStateFlag::TSF_ON_ICE) | ||
? static_cast< int >( new_direction ) | ||
: static_cast< int >( direction ) | ||
) | ||
, m_current_frame + 2 * star_count); | ||
else | ||
src_rect = moveRect(m_sprite->rect, 0, m_current_frame + 2 * star_count); | ||
stop = false; | ||
} | ||
}; | ||
|
||
class NetGame : public ::Game { | ||
void update(Uint32 dt) override; | ||
|
||
// TODO(alex): uglyAndFast, omitt `static`, delete in App::run | ||
inline static std::shared_ptr< NetPlayer > m_player; | ||
|
||
public: | ||
/** | ||
* Allows multi-player | ||
*/ | ||
NetGame(int players_count) : | ||
// tmp | ||
Game( 1 ) | ||
{} | ||
}; | ||
} // namespace net |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.