Simulant  21.12-574
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
fly.h
1 /* * Copyright (c) 2011-2017 Luke Benstead https://simulant-engine.appspot.com
2  *
3  * This file is part of Simulant.
4  *
5  * Simulant is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Simulant is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with Simulant. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #pragma once
20 
21 #include "./behaviour.h"
22 #include "../window.h"
23 
24 #include "../interfaces/transformable.h"
25 
26 namespace smlt {
27 namespace behaviours {
28 
29 class Fly:
30  public BehaviourWithInput,
31  public RefCounted<Fly> {
32 
33 public:
34  Fly(Window* window):
35  BehaviourWithInput(window->input.get()),
36  window_(window) {
37  }
38 
39  const char* name() const override { return "Fly by Keyboard"; }
40 
41  void set_speed(float v) { speed_ = v; }
42 private:
43  void on_behaviour_added(Organism* controllable) override {
44  stage_node_ = dynamic_cast<StageNode*>(controllable);
45 
46  window_->lock_cursor();
47  }
48 
49  void on_behaviour_removed(Organism *controllable) override {
50  _S_UNUSED(controllable);
51  stage_node_ = nullptr;
52  }
53 
54  void late_update(float dt) override {
55  if(!attached()) {
56  return;
57  }
58 
59  stage_node_->move_forward_by(input->axis_value("Vertical") * speed_ * dt);
60  stage_node_->rotate_global_y_by(Degrees(input->axis_value("Horizontal") * -50.0f * dt));
61  stage_node_->rotate_global_y_by(Degrees(input->axis_value("MouseX") * -50.0f * dt));
62  stage_node_->rotate_x_by(Degrees(input->axis_value("MouseY") * -50.0f * dt));
63  }
64 
65  StageNode* stage_node_ = nullptr;
66  Window* window_ = nullptr;
67  float speed_ = 600.0f;
68 };
69 
70 }
71 }
smlt::Organism
Definition: behaviour.h:132
smlt::RefCounted
Definition: managed.h:65
smlt
Definition: animation.cpp:25
smlt::Window
Definition: window.h:65
smlt::Degrees
Definition: degrees.h:7
smlt::StageNode
Definition: stage_node.h:62
smlt::BehaviourWithInput
Definition: behaviour.h:100
smlt::behaviours::Fly
Definition: fly.h:31