Joshua Moerman
12 years ago
3 changed files with 108 additions and 22 deletions
@ -1 +1 @@ |
|||||
Subproject commit 018cb176c4b3970cd2a6ecae8a63520183c5ddd3 |
Subproject commit 61c889b0c697f522a66a7bfaf002cd143d018060 |
@ -1,42 +1,102 @@ |
|||||
#pragma once |
#pragma once |
||||
|
|
||||
|
#include <iostream> |
||||
|
#include <cstdlib> |
||||
#include <chrono> |
#include <chrono> |
||||
#include <cmath> |
#include <cmath> |
||||
#include <vector> |
#include <vector> |
||||
#include <algorithm> |
#include <algorithm> |
||||
|
|
||||
#include "basics.h" |
#include "basics.h" |
||||
|
#include "adaptions.h" |
||||
|
|
||||
|
#include "simulation/Simulation.h" |
||||
|
#include "simulation/Beat.h" |
||||
|
|
||||
|
using Vec2 = math::Vec2; |
||||
|
using LineKind = simulation::LineKind; |
||||
|
|
||||
|
using ball_info = int; |
||||
|
using line_info = void; |
||||
|
using ball_type = simulation::Ball<ball_info>; |
||||
|
using line_type = simulation::Line<line_info>; |
||||
|
using simu_type = simulation::Simulation<ball_info, line_info>; |
||||
|
|
||||
|
using note_info = Vec2; |
||||
|
using note_type = Note<note_info>; |
||||
|
using beat_type = Beat<note_info>; |
||||
|
|
||||
|
struct AbstractLine { |
||||
|
math::Vec2 starting_point; |
||||
|
math::Vec2 end_point; |
||||
|
LineKind line_kind; |
||||
|
|
||||
|
const float hsize = 5.0f; |
||||
|
|
||||
|
AbstractLine(math::Vec2 starting_point, math::Vec2 end_point, LineKind line_kind) |
||||
|
: starting_point(starting_point) |
||||
|
, end_point(end_point) |
||||
|
, line_kind(line_kind) |
||||
|
{} |
||||
|
|
||||
|
// create 6 lines, to emulate width, and rounded edges
|
||||
|
std::vector<line_type> calculate_lines() const { |
||||
|
auto dir = normalize(end_point - starting_point); |
||||
|
auto normal = rotate_ccw(dir); |
||||
|
|
||||
|
if(line_kind == simulation::kOneWay){ |
||||
|
std::vector<line_type> ret; |
||||
|
ret.emplace_back(starting_point + hsize*normal, end_point + hsize*normal, line_kind); |
||||
|
ret.emplace_back(end_point + hsize*normal, end_point + hsize*dir, line_kind); |
||||
|
ret.emplace_back(end_point + hsize*dir, end_point - hsize*normal, line_kind); |
||||
|
ret.emplace_back(end_point - hsize*normal, starting_point - hsize*normal, line_kind); |
||||
|
ret.emplace_back(starting_point - hsize*normal, starting_point - hsize*dir, line_kind); |
||||
|
ret.emplace_back(starting_point - hsize*dir, starting_point + hsize*normal, line_kind); |
||||
|
return ret; |
||||
|
} else { |
||||
|
std::vector<line_type> ret; |
||||
|
ret.emplace_back(starting_point, end_point, line_kind); |
||||
|
return ret; |
||||
|
} |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
struct libwebsocket; |
||||
struct App{ |
struct App{ |
||||
std::vector<User*> people_online; |
std::vector<User*> people_online; |
||||
int uid{0}; |
libwebsocket * uberclient{nullptr}; |
||||
|
|
||||
void update_positions(){ |
simu_type sim; |
||||
const float speed = 0.4; |
beat_type beat; |
||||
|
|
||||
typedef std::chrono::duration<double, std::ratio<1,1>> fseconds; |
const float iframerate = 1.0/60.0; |
||||
static auto start = std::chrono::steady_clock::now(); |
int peopleuid{0}; |
||||
auto end = std::chrono::steady_clock::now(); |
int balluid{0}; |
||||
double time = std::chrono::duration_cast<fseconds>(end-start).count(); |
|
||||
|
|
||||
//std::cout << time << std::endl;
|
App(){ |
||||
auto n = people_online.size(); |
beat.notes.emplace_back(note_type::kQuarterNote, note_info{100.0f, 100.0f}); |
||||
for(int i = 0; i < n; ++i){ |
|
||||
User& user = *people_online[i]; |
|
||||
double x = cos(speed * time + 2*M_PI*i/double(n)); |
|
||||
double y = sin(speed * time + 2*M_PI*i/double(n)); |
|
||||
|
|
||||
user.position.x = 200 + 200*x; |
AbstractLine line(Vec2{50.0f, 100.0f}, Vec2{150.0f, 200.0f}, simulation::kOneWay); |
||||
user.position.y = 200 + 200*y; |
for(auto & l : line.calculate_lines()){ |
||||
|
sim.lines.push_back(l); |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
void login(User& user){ |
void login(User& user){ |
||||
user.index = uid++; |
user.index = peopleuid++; |
||||
people_online.push_back(&user); |
people_online.push_back(&user); |
||||
} |
} |
||||
|
|
||||
void logout(User& user){ |
void logout(User& user){ |
||||
people_online.erase(std::remove(people_online.begin(), people_online.end(), &user), people_online.end()); |
people_online.erase(std::remove(people_online.begin(), people_online.end(), &user), people_online.end()); |
||||
} |
} |
||||
|
|
||||
|
void update(){ |
||||
|
float dt = iframerate; |
||||
|
sim.update(dt); |
||||
|
|
||||
|
for(auto n : beat.update(dt)){ |
||||
|
sim.balls.emplace_back(n.x, n.y, 0, 0, balluid++); |
||||
|
} |
||||
|
} |
||||
}; |
}; |
Reference in new issue