Created
December 5, 2016 04:13
-
-
Save ChunChunMorning/2be48a2a2253ca79cb1751c023366318 to your computer and use it in GitHub Desktop.
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
class Game : public SceneBase<String, GameData> | |
{ | |
private: | |
bool isParent; | |
bool isSendGoal; | |
Rect leftBar; | |
int leftBarSpeed; | |
Rect rightBar; | |
int rightBarSpeed; | |
Circle ball; | |
Vec2 ballSpeed; | |
String buffer; | |
int getInput() | |
{ | |
int move = 0; | |
if (Input::KeyUp.pressed) | |
move -= 1; | |
if (Input::KeyDown.pressed) | |
move += 1; | |
return move; | |
} | |
public: | |
void init() override | |
{ | |
isParent = false; | |
isSendGoal = false; | |
leftBar = Rect(50, 180, 30, 120); | |
rightBar = Rect(560, 180, 30, 120); | |
ball = Circle(320, 240, 10); | |
ballSpeed = Vec2(-1, 1); | |
} | |
void update() override | |
{ | |
if (m_data->client.hasError()) | |
{ | |
m_data->client.disconnect(); | |
changeScene(L"connecting"); | |
return; | |
} | |
m_data->client.sendString(Format(L"move,", getInput(), L'\n')); | |
while (m_data->client.readLine(buffer)) | |
{ | |
buffer.pop_back(); | |
const auto args = buffer.split(L','); | |
if (args[0] == L"isParent") | |
{ | |
isParent = true; | |
} | |
else if (args[0] == L"parent") | |
{ | |
leftBarSpeed = Parse<int>(args[1]); | |
} | |
else if (args[0] == L"child") | |
{ | |
rightBarSpeed = Parse<int>(args[1]); | |
} | |
else if (args[0] == L"goal") | |
{ | |
ball.center = Window::Center(); | |
isSendGoal = false; | |
} | |
} | |
ball.center += 2.0 * ballSpeed; | |
leftBar.y += 3 * leftBarSpeed; | |
rightBar.y += 3 * rightBarSpeed; | |
if (ball.center.y < ball.r || ball.center.y > Window::Height() - ball.r) | |
{ | |
ballSpeed.y = -ballSpeed.y; | |
} | |
if (ball.intersects(leftBar) || ball.intersects(rightBar)) | |
{ | |
ballSpeed.x = -ballSpeed.x; | |
} | |
if (!isSendGoal && isParent && (ball.center.x < -ball.r || Window::Width() + ball.r < ball.center.x)) | |
{ | |
m_data->client.sendString(Format(L"goal\n")); | |
isSendGoal = true; | |
} | |
} | |
void draw() const override | |
{ | |
ball.draw(Palette::Yellow); | |
leftBar.draw(isParent ? Palette::Red : Palette::White); | |
rightBar.draw(isParent ? Palette::White : Palette::Red); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment