implemented a new object factory mechanism which is now really independent of the...
[supertux.git] / src / object / rock.cpp
1 #include <config.h>
2
3 #include "rock.h"
4 #include "special/sprite.h"
5 #include "special/sprite_manager.h"
6 #include "lisp/writer.h"
7 #include "video/drawing_context.h"
8 #include "resources.h"
9 #include "object_factory.h"
10
11 Rock::Rock(const lisp::Lisp& reader)
12 {
13   reader.get("x", bbox.p1.x);
14   reader.get("y", bbox.p1.y);
15   bbox.set_size(31.8, 31.8);
16   sprite = sprite_manager->create("rock");
17   grabbed = false;
18   flags |= FLAG_SOLID;
19 }
20
21 Rock::~Rock()
22 {
23   delete sprite;
24 }
25
26 void
27 Rock::write(lisp::Writer& writer)
28 {
29   writer.start_list("rock");
30
31   writer.write_float("x", bbox.p1.x);
32   writer.write_float("y", bbox.p1.y);
33
34   writer.end_list("rock");
35 }
36
37 void
38 Rock::draw(DrawingContext& context)
39 {
40
41   sprite->draw(context, get_pos(), LAYER_OBJECTS);
42 }
43
44 void
45 Rock::action(float elapsed_time)
46 {
47   if(!grabbed) {
48     flags |= FLAG_SOLID;
49     flags &= ~FLAG_NO_COLLDET;
50     movement = physic.get_movement(elapsed_time);
51   } else {
52     physic.set_velocity(0, 0);
53     flags &= ~FLAG_SOLID;
54     flags |= FLAG_NO_COLLDET;
55   }
56   
57   grabbed = false;
58 }
59
60 HitResponse
61 Rock::collision(GameObject& , const CollisionHit& )
62 {
63   if(grabbed)
64     return FORCE_MOVE;
65
66   physic.set_velocity(0, 0);
67   return CONTINUE;
68 }
69
70 void
71 Rock::grab(MovingObject& , const Vector& pos)
72 {
73   movement = pos - get_pos();
74   grabbed = true;
75 }
76
77 IMPLEMENT_FACTORY(Rock, "rock")
78