-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.cpp
More file actions
78 lines (69 loc) · 1.67 KB
/
Node.cpp
File metadata and controls
78 lines (69 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "Core.h"
#include "Node.h"
#include <assert.h>
Node::Node()
:pos(sf::Vector2f(0, 0)), selected(false)
{
shape.setFillColor(sf::Color::Red);
shape.setPosition(pos);
shape.setRadius(4);
shape.setOrigin({ shape.getRadius(),shape.getRadius() });
}
Node::Node(sf::Vector2f position)
:pos(position), selected(false)
{
shape.setFillColor(sf::Color::Red);
shape.setPosition(pos);
shape.setRadius(4);
shape.setOrigin({ shape.getRadius(),shape.getRadius() });
}
Node::Node(sf::Vector2f position, bool isStatic)
:pos(position), isStatic(isStatic), selected(false)
{
if (isStatic)
{
shape.setFillColor(sf::Color::Blue);
}
else
{
shape.setFillColor(sf::Color::Red);
}
shape.setPosition(pos);
shape.setRadius(4);
shape.setOrigin({ shape.getRadius(),shape.getRadius() });
}
void Node::draw(sf::RenderWindow& window)
{
window.draw(this->shape);
}
void Node::update(sf::RenderWindow& window)
{
if (!isStatic)
{
if (selected)
{
sf::Vector2f mousePos = window.mapPixelToCoords(sf::Mouse::getPosition(window));
sf::Vector2f force = sf::Vector2f(mousePos.x - pos.x, mousePos.y - pos.y);
applyForce(force * 0.1f);
}
applyForce(gravity * 0.3f);
Core::clamp(vel, -50.0f, 50.0f);
pos += sf::Vector2f(vel.x * (float)timestep, vel.y * (float)timestep);
shape.setPosition(pos);
}
}
void Node::applyForce(sf::Vector2f force)
{
vel += sf::Vector2f(force.x * (float)timestep, force.y * (float)timestep);
}
void Node::select()
{
selected = true;
shape.setOutlineThickness(1);
shape.setOutlineColor(sf::Color::Green);
}
void Node::deselect()
{
selected = false;
shape.setOutlineThickness(0);
}