-
Notifications
You must be signed in to change notification settings - Fork 0
/
character.cpp
124 lines (109 loc) · 3.32 KB
/
character.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "character.h"
#include <fstream>
#include <sstream>
Character::Character( const std::string &name, unsigned int player ) try
: _name( name ),
_cur_health( 100 ),
_textures( name ),
_state( IDLE )
{
// initialize the current execution_position array to a value not used
std::fill_n( _execution_position, HIT + 1, 99 );
// set initial position
_execution_position[IDLE] = 0;
// this will be the starting image
std::string img_name = ( ( player == 1 ) ? "idle1" : "idle2" );
// set the initial character texture to idle
_character_texture =
std::make_shared<sf::Texture>( _textures.get_texture( img_name, 0 ) );
// create the map that holds how many sub images there are per image
set_position_counts( name );
_image_names[IDLE] = "idle";
_image_names[WALKING] = "walk";
_image_names[JUMPING] = "jump";
_image_names[PUNCHING] = "punch";
_image_names[KICKING] = "kick";
_image_names[CROUCHING] = "crouch";
_image_names[HIT] = "hit";
}
catch ( char const *e )
{
throw e;
}
Character::~Character( void )
{
}
void Character::update( const STATE &state, char facing )
{
// get the string for the image to match the state
std::string img_name = _image_names[state];
// if we are facing right, append a 1 to the img_name, else append a 2
if ( facing == 'R' )
img_name.append( "1" );
else
img_name.append( "2" );
// if we are still in the same state
if ( state == _state )
{
// if the execution_position is less than max, increment it. Else reset it
// to IDLE
if ( _execution_position[state] < _position_counts[img_name] - 1 )
{
++_execution_position[state];
}
else // we have finished the loop of whatever image we are on
{
// set the current state to not being used
//_execution_position[state] = 99;
// reset the IDLE state to the beggining of the loop and change to IDLE
_execution_position[state] = 0;
_state = IDLE;
}
}
else // the new state doesn't match the current state
{
_execution_position[state] = 0;
_execution_position[_state] = 0;
// decrement health if character was hit
if ( state == HIT )
_cur_health -= 10;
// start the image loop counter for the state we are in
_state = state;
}
// set the new current texture
_character_texture = std::make_shared<sf::Texture>(
_textures.get_texture( img_name, _execution_position[_state] ) );
}
float Character::get_health( void )
{
return _cur_health;
}
std::shared_ptr<sf::Texture> Character::get_texture( void )
{
return _character_texture;
}
std::pair<STATE, unsigned int> Character::get_state( void )
{
return std::make_pair( _state, _execution_position[_state] );
}
void Character::set_position_counts( const std::string &name )
{
// read in the position_counts for the character, there are only 7 with 2
// counts per position (right and left)
std::string file = "assets/" + name + "/position_counts.txt";
std::ifstream ifs;
ifs.open( file );
std::string position;
unsigned int count;
// might change to while getline in case more images are added later
for ( int i = 0; i < 14; ++i )
{
std::string line;
getline( ifs, line );
std::stringstream ss( line );
ss >> position;
ss >> count;
_position_counts.insert( std::make_pair( position, count ) );
}
ifs.close();
}