Skip to content

Commit

Permalink
Added basic animation for the player
Browse files Browse the repository at this point in the history
Can only facing north at the moment.

There is a bug on the controls. It looks like while having a certain key
pressed, and adding a new key press and releasing it will cause it to
stop sending packets for new updates. Currently looking into the
conditions...
  • Loading branch information
Chibin committed Dec 3, 2013
1 parent 88954c6 commit e86c319
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
12 changes: 10 additions & 2 deletions SFML with RakNet/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ class Player : public Entity{
sf::Clock clock;
sf::Texture spriteSheet;
sf::Sprite playerSprite;
int currentFrame;

public:
Player(){
spritePath = "Images/Lloyd.png";
setPosition(50.0,50.0);
currentFrame = 0;
}
void playerInit(std::string playerName=""){
if(playerName == ""){
Expand All @@ -35,9 +37,15 @@ class Player : public Entity{
std::string getName() { std::cout << "returning the name: " << name << std::endl; return name;}
void draw(sf::RenderWindow &window){
playerSprite.setPosition(currentPos.x,currentPos.y);
printf("%f %f \n",currentPos.x,currentPos.y);
//printf("%f %f \n",currentPos.x,currentPos.y);
spriteSheet.loadFromFile(spritePath);
playerSprite.setTexture(spriteSheet);
//playerSprite.setTexture(spriteSheet);
sf::IntRect test(0+24*currentFrame,0+32*0,24,32);
if( clock.getElapsedTime().asMilliseconds() > 450){
clock.restart();
currentFrame = currentFrame < 2? currentFrame+1:0;
}
playerSprite.setTextureRect(test);
window.draw(playerSprite);
}
position getPosition(){
Expand Down
2 changes: 1 addition & 1 deletion SFML with RakNet/clientPacketHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class clientPacketHandler : public packetHandler{
bsIn.IgnoreBytes(sizeof(RakNet::MessageID));
float posx, posy;
bsIn.Read(posx); bsIn.Read(posy);
printf("New position: %f %f\n",posx,posy);
//printf("New position: %f %f\n",posx,posy);
player->setPosition(posx,posy);
}
break;
Expand Down
17 changes: 11 additions & 6 deletions SFML with RakNet/serverclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,12 @@ class serverclient{
int y;
if (event.type == sf::Event::KeyReleased){
if (event.key.code == sf::Keyboard::LShift || event.key.code == sf::Keyboard::RShift) isShiftDown = false;
if(event.key.code == sf::Keyboard::Up) isUp = false;
if(event.key.code == sf::Keyboard::Down) isDown = false;
if(event.key.code == sf::Keyboard::Left) isLeft = false;
if(event.key.code == sf::Keyboard::Right) isRight = false;
if(event.key.code == sf::Keyboard::Up) { isUp = false; printf("UP release\n");}
if(event.key.code == sf::Keyboard::Down) { isDown = false; printf("DOWN release\n");}
if(event.key.code == sf::Keyboard::Left) { isLeft = false; printf("LEFT release\n");}
if(event.key.code == sf::Keyboard::Right) {isRight = false; printf("RIGHT release\n"); }
}

if (event.type == sf::Event::KeyPressed){
window.setKeyRepeatEnabled(repeat);
if (event.key.code == sf::Keyboard::Return){
Expand Down Expand Up @@ -189,12 +190,16 @@ class serverclient{
else if(event.key.code == sf::Keyboard::Down){ y = 2; isDown = true;}
else if(event.key.code == sf::Keyboard::Left){ isLeft = true; y = 3; }
else if(event.key.code == sf::Keyboard::Right){ isRight = true; y = 1; }
std::cout << event.type << " " <<keypressToChar(event.key.code) << std::endl;
else std::cout << event.type << " " <<keypressToChar(event.key.code) << std::endl;
}
//-----------
//character calculations: Request to move the character
//------------
if(isUp || isDown || isLeft || isRight) {//player.move(0,-3);
if(isUp) printf("UP!\n");
if(isDown) printf("isDown!\n");
if(isLeft) printf("isLeft!\n");
if(isRight) printf("isRight!\n");
if(isUp==true || isDown==true || isLeft==true || isRight==true) {//player.move(0,-3);
BitStream bsOut;
bsOut.Write((RakNet::MessageID)REQUEST_FOR_PLAYER_TO_MOVE);
std::string playerName = player->getName().c_str();
Expand Down

0 comments on commit e86c319

Please sign in to comment.