Skip to content

Commit c9750f4

Browse files
committed
Add push lasers, they kill you in a different way.
1 parent a1404be commit c9750f4

File tree

5 files changed

+46
-5
lines changed

5 files changed

+46
-5
lines changed

src/laser.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ void Laser::onFixedUpdate()
5454
if (!object->isSolid())
5555
return true;
5656
laser_vector = hit_location - getPosition2D();
57-
if (sp::P<Spaceship>(object))
58-
(sp::P<Spaceship>(object))->explode();
57+
onHit(object, hit_location);
5958
return false;
6059
});
6160
render_data.scale.x = laser_vector.x;
@@ -73,8 +72,8 @@ void Laser::onFixedUpdate()
7372
parameters.velocity.x = sp::random(-7, 7);
7473
parameters.velocity.y = sp::random(-7, 7);
7574
parameters.acceleration = -parameters.velocity;
76-
parameters.start_color = sp::Color(1, 0.5, 0.5);
77-
parameters.end_color = sp::Color(1, 0, 0, 0);
75+
parameters.start_color = render_data.color;
76+
parameters.end_color = sp::Color(0, 0, 0, 0);
7877

7978
parameters.start_size = 3.0;
8079
parameters.end_size = 5.0;
@@ -91,3 +90,9 @@ void Laser::setProperty(sp::string name, sp::string value)
9190
else
9291
TriggerableNode::setProperty(name, value);
9392
}
93+
94+
void Laser::onHit(sp::P<sp::Node> object, sp::Vector2d hit_location)
95+
{
96+
if (sp::P<Spaceship>(object))
97+
(sp::P<Spaceship>(object))->explode();
98+
}

src/laser.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ class Laser : public TriggerableNode
1515
virtual void onFixedUpdate() override;
1616

1717
virtual void setProperty(sp::string name, sp::string value) override;
18-
private:
18+
protected:
19+
virtual void onHit(sp::P<sp::Node> object, sp::Vector2d hit_location);
20+
1921
sp::Vector2d aim_vector;
22+
private:
2023
bool active;
2124
int sparkdelay;
2225

src/levelLoader.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "spaceship.h"
66
#include "door.h"
77
#include "laser.h"
8+
#include "pushLaser.h"
89
#include "physicsObject.h"
910
#include "trigger.h"
1011
#include "timer.h"
@@ -108,6 +109,11 @@ void loadLevel(sp::P<sp::Node> root, sp::string name)
108109
obj = new Laser(root, object["name"].string_value());
109110
obj->setPosition(position);
110111
}
112+
else if (object["type"] == "PUSHLASER")
113+
{
114+
obj = new PushLaser(root, object["name"].string_value());
115+
obj->setPosition(position);
116+
}
111117
else if (object["type"] == "TRIGGER")
112118
{
113119
float w = object["width"].number_value();

src/pushLaser.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "pushLaser.h"
2+
3+
PushLaser::PushLaser(sp::P<sp::Node> parent, sp::string trigger)
4+
: Laser(parent, trigger)
5+
{
6+
render_data.color = sp::Color(0.5, 0.5, 1.0);
7+
}
8+
9+
void PushLaser::onHit(sp::P<sp::Node> object, sp::Vector2d hit_location)
10+
{
11+
object->setLinearVelocity(object->getLinearVelocity2D() + aim_vector * 0.001);
12+
}

src/pushLaser.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef PUSH_LASER_H
2+
#define PUSH_LASER_H
3+
4+
#include "laser.h"
5+
6+
class PushLaser : public Laser
7+
{
8+
public:
9+
PushLaser(sp::P<sp::Node> parent, sp::string trigger);
10+
11+
protected:
12+
virtual void onHit(sp::P<sp::Node> object, sp::Vector2d hit_location) override;
13+
};
14+
15+
#endif//PUSH_LASER_H

0 commit comments

Comments
 (0)