forgot to add files
authorMatthias Braun <matze@braunis.de>
Fri, 6 May 2005 14:55:45 +0000 (14:55 +0000)
committerMatthias Braun <matze@braunis.de>
Fri, 6 May 2005 14:55:45 +0000 (14:55 +0000)
SVN-Revision: 2417

src/object/scripted_object.cpp [new file with mode: 0644]
src/object/scripted_object.h [new file with mode: 0644]

diff --git a/src/object/scripted_object.cpp b/src/object/scripted_object.cpp
new file mode 100644 (file)
index 0000000..82f72e9
--- /dev/null
@@ -0,0 +1,148 @@
+#include <config.h>
+
+#include <stdexcept>
+
+#include "scripted_object.h"
+#include "video/drawing_context.h"
+#include "sprite/sprite_manager.h"
+#include "resources.h"
+#include "object_factory.h"
+#include "math/vector.h"
+
+ScriptedObject::ScriptedObject(const lisp::Lisp& lisp)
+  : solid(true), physic_enabled(true), visible(true)
+{
+  lisp.get("name", name);
+  if(name == "")
+    throw std::runtime_error("Scripted object must have a name specified");
+  
+  std::string spritename;
+  lisp.get("sprite", spritename);
+  if(spritename == "")
+    throw std::runtime_error("Scripted object must have a sprite name specified");
+  sprite = sprite_manager->create(spritename);
+
+  lisp.get("x", bbox.p1.x);
+  lisp.get("y", bbox.p1.y);
+  float width = sprite->get_width();
+  float height = sprite->get_height();
+  lisp.get("width", width);
+  lisp.get("height", height);
+  bbox.set_size(width, height);
+
+  lisp.get("solid", solid);
+  lisp.get("physic-enabled", physic_enabled);
+  lisp.get("visible", visible);
+  if(solid)
+    flags |= FLAG_SOLID;
+}
+
+ScriptedObject::~ScriptedObject()
+{
+  delete sprite;
+}
+
+void
+ScriptedObject::move(float x, float y)
+{
+  bbox.move(Vector(x, y));
+}
+
+void
+ScriptedObject::set_pos(float x, float y)
+{
+  bbox.set_pos(Vector(x, y));
+}
+
+float
+ScriptedObject::get_pos_x()
+{
+  return get_pos().x;
+}
+
+float
+ScriptedObject::get_pos_y()
+{
+  return get_pos().y;
+}
+
+void
+ScriptedObject::set_velocity(float x, float y)
+{
+  physic.set_velocity(x, y);
+}
+
+float
+ScriptedObject::get_velocity_x()
+{
+  return physic.get_velocity_x();
+}
+
+float
+ScriptedObject::get_velocity_y()
+{
+  return physic.get_velocity_y();
+}
+
+void
+ScriptedObject::set_visible(bool visible)
+{
+  this->visible = visible;
+}
+
+bool
+ScriptedObject::is_visible()
+{
+  return visible;
+}
+
+void
+ScriptedObject::set_animation(const std::string& animation)
+{
+  sprite->set_action(animation);
+}
+
+std::string
+ScriptedObject::get_animation()
+{
+  return sprite->get_action_name();
+}
+
+std::string
+ScriptedObject::get_name()
+{
+  return name;
+}
+
+void
+ScriptedObject::action(float elapsed_time)
+{
+  if(!physic_enabled)
+    return;
+
+  movement = physic.get_movement(elapsed_time);
+}
+
+void
+ScriptedObject::draw(DrawingContext& context)
+{
+  if(!visible)
+    return;
+
+  sprite->draw(context, get_pos(), LAYER_OBJECTS);
+}
+
+HitResponse
+ScriptedObject::collision(GameObject& other, const CollisionHit& )
+{
+  if(!physic_enabled)
+    return FORCE_MOVE;
+
+  if(!(other.get_flags() & FLAG_SOLID))
+    return FORCE_MOVE;
+
+  physic.set_velocity(0, 0);
+  return CONTINUE;
+}
+
+IMPLEMENT_FACTORY(ScriptedObject, "scriptedobject");
diff --git a/src/object/scripted_object.h b/src/object/scripted_object.h
new file mode 100644 (file)
index 0000000..4ee0500
--- /dev/null
@@ -0,0 +1,48 @@
+#ifndef __SCRIPTED_OBJECT_H__
+#define __SCRIPTED_OBJECT_H__
+
+#include <string>
+#include "physic.h"
+#include "sprite/sprite.h"
+#include "lisp/lisp.h"
+#include "moving_object.h"
+#include "scripting/scripted_object.h"
+
+class ScriptedObject : public MovingObject, public Scripting::ScriptedObject
+{
+public:
+  ScriptedObject(const lisp::Lisp& lisp);
+  virtual ~ScriptedObject();
+
+  void action(float elapsed_time);
+  void draw(DrawingContext& context);
+  HitResponse collision(GameObject& other, const CollisionHit& hit);
+
+  // --- Scripting Interface stuff ---
+
+  void set_animation(const std::string& animation);
+  std::string get_animation();
+
+  void move(float x, float y);
+  void set_pos(float x, float y);
+  float get_pos_x();
+  float get_pos_y();
+  void set_velocity(float x, float y);
+  float get_velocity_x();
+  float get_velocity_y();
+  void set_visible(bool visible);
+  bool is_visible();
+
+  std::string get_name();
+
+private:
+  std::string name;
+  bool solid;
+  bool physic_enabled;
+  bool visible;
+  Physic physic;
+  Sprite* sprite;
+};
+
+#endif
+