changeset 146:91e57baa2ede

Client: add prototype for map state, #712
author David Demelier <markand@malikania.fr>
date Sat, 30 Sep 2017 13:35:45 +0200
parents c8510782d40d
children ba8642323700
files libclient/CMakeLists.txt libclient/malikania/client/state/lobby_state.cpp libclient/malikania/client/state/map_state.cpp libclient/malikania/client/state/map_state.hpp
diffstat 4 files changed, 122 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libclient/CMakeLists.txt	Sat Sep 30 11:32:34 2017 +0200
+++ b/libclient/CMakeLists.txt	Sat Sep 30 13:35:45 2017 +0200
@@ -40,6 +40,7 @@
     ${libmlk-client_SOURCE_DIR}/malikania/client/rectangle.hpp
     ${libmlk-client_SOURCE_DIR}/malikania/client/sprite.hpp
     ${libmlk-client_SOURCE_DIR}/malikania/client/state.hpp
+    ${libmlk-client_SOURCE_DIR}/malikania/client/state/map_state.hpp
     ${libmlk-client_SOURCE_DIR}/malikania/client/state/login_state.hpp
     ${libmlk-client_SOURCE_DIR}/malikania/client/state/lobby_state.hpp
     ${libmlk-client_SOURCE_DIR}/malikania/client/state/splashscreen_state.hpp
@@ -65,6 +66,7 @@
     ${libmlk-client_SOURCE_DIR}/malikania/client/image.cpp
     ${libmlk-client_SOURCE_DIR}/malikania/client/label.cpp
     ${libmlk-client_SOURCE_DIR}/malikania/client/sprite.cpp
+    ${libmlk-client_SOURCE_DIR}/malikania/client/state/map_state.cpp
     ${libmlk-client_SOURCE_DIR}/malikania/client/state/login_state.cpp
     ${libmlk-client_SOURCE_DIR}/malikania/client/state/lobby_state.cpp
     ${libmlk-client_SOURCE_DIR}/malikania/client/state/splashscreen_state.cpp
--- a/libclient/malikania/client/state/lobby_state.cpp	Sat Sep 30 11:32:34 2017 +0200
+++ b/libclient/malikania/client/state/lobby_state.cpp	Sat Sep 30 13:35:45 2017 +0200
@@ -19,6 +19,7 @@
 #include "client.hpp"
 #include "color.hpp"
 #include "lobby_state.hpp"
+#include "map_state.hpp"
 #include "theme.hpp"
 #include "window.hpp"
 
@@ -38,6 +39,8 @@
         index_ = index_ == names_.size() - 1 ? 0 : index_ + 1;
     else if (ev.key == key::up)
         index_ = index_ == 0 ? names_.size() - 1 : index_ - 1;
+    else
+        client_.set_state(std::make_unique<map_state>());
 }
 
 void lobby_state::update(client&)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libclient/malikania/client/state/map_state.cpp	Sat Sep 30 13:35:45 2017 +0200
@@ -0,0 +1,64 @@
+#include "client.hpp"
+#include "color.hpp"
+#include "map_state.hpp"
+#include "point.hpp"
+#include "rectangle.hpp"
+#include "theme.hpp"
+#include "window.hpp"
+
+namespace mlk {
+
+namespace client {
+
+void map_state::handle_key_down(const key_event& ev)
+{
+    if (ev.key == key::down)
+        delta_ = {delta_.x(), 1};
+    else if (ev.key == key::up)
+        delta_ = {delta_.x(), -1};
+    else if (ev.key == key::left)
+        delta_ = {-1, delta_.y()};
+    else if (ev.key == key::right)
+        delta_ = {1, delta_.y()};
+}
+
+void map_state::handle_key_up(const key_event& ev)
+{
+    if (ev.key == key::down || ev.key == key::up)
+        delta_ = {delta_.x(), 0};
+    else if (ev.key == key::left || ev.key == key::right)
+        delta_ = {0, delta_.y()};
+}
+
+void map_state::update(client& clt)
+{
+    position_ = {
+        position_.x() + delta_.x(),
+        position_.y() + delta_.y()
+    };
+}
+
+void map_state::draw(client& clt)
+{
+    auto& win = clt.window();
+
+    win.set_drawing_color(0xffffffff);
+    win.clear();
+    win.set_drawing_color(0xff000000);
+
+    // Draw a little square as user position.
+    win.fill_rectangle({position_.x(), position_.y(), 10, 10});
+
+    // Draw nickname on top of it.
+    auto clip = win.theme().font().clip("molko");
+
+    win.draw_text("molko", win.theme().font(), mlk::client::point(
+        position_.x() - (clip.width() / 2),
+        position_.y() - clip.height() - 10
+    ));
+    win.present();
+}
+
+} // !client
+
+} // !mlk
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libclient/malikania/client/state/map_state.hpp	Sat Sep 30 13:35:45 2017 +0200
@@ -0,0 +1,53 @@
+/*
+ * map_state.hpp -- main game state
+ *
+ * Copyright (c) 2013-2017 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef MALIKANIA_CLIENT_MAP_STATE_HPP
+#define MALIKANIA_CLIENT_MAP_STATE_HPP
+
+/**
+ * \file map_state.hpp
+ * \brief Main game state.
+ */
+
+#include "point.hpp"
+#include "state.hpp"
+
+namespace mlk {
+
+namespace client {
+
+/**
+ * \brief Main game state.
+ */
+class map_state : public state {
+private:
+    point position_{320, 240};
+    point delta_;
+
+public:
+    void update(client&) override;
+    void draw(client& clt) override;
+    void handle_key_down(const key_event& ev) override;
+    void handle_key_up(const key_event& ev) override;
+};
+
+} // !client
+
+} // !mlk
+
+#endif // !MALIKANIA_CLIENT_MAP_STATE_HPP