Angel 3.2
A 2D Game Prototyping Engine
Actor.h
Go to the documentation of this file.
1 
2 // Copyright (C) 2008-2013, Shane Liesegang
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of the copyright holder nor the names of any
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
29 
31 #pragma once
32 
33 #include "../Infrastructure/Renderable.h"
34 #include "../Infrastructure/Color.h"
35 #include "../Infrastructure/Interval.h"
36 #include "../Messaging/Message.h"
37 
38 #define MAX_SPRITE_FRAMES 64
39 #define CIRCLE_DRAW_SECTIONS 32
40 
45 {
46  SAT_None,
47  SAT_Loop,
48  SAT_PingPong,
49  SAT_OneShot
50 };
51 
57 {
58  ADS_Square,
59  ADS_Circle,
60  ADS_CustomList
61 };
62 
64 
75 class Actor : public Renderable, public MessageListener
76 {
77 public:
83  Actor();
84 
90  virtual ~Actor();
91 
100  void SetSize(float x, float y = -1.f); // equal dimensions by default
101 
108  void SetSize(const Vector2& newSize);
109 
115  const Vector2& GetSize() const;
116 
122  const BoundingBox GetBoundingBox() const;
123 
130  virtual void SetPosition(float x, float y);
131 
137  virtual void SetPosition(const Vector2& pos);
138 
144  const Vector2& GetPosition() const;
145 
152  virtual void SetRotation(float rotation);
153 
159  const float GetRotation() const;
160 
169  void SetColor(float r, float g, float b, float a=1.0f);
170 
176  void SetColor(const Color& color);
177 
183  const Color& GetColor() const;
184 
192  void SetAlpha(float newAlpha);
193 
199  const float GetAlpha() const;
200 
207  virtual void SetDrawShape( actorDrawShape drawShape );
208 
214  const actorDrawShape& GetDrawShape() const;
215 
221  void UseDisplayList(int listIndex)
222  {
223  _displayListIndex = listIndex;
224  SetDrawShape(ADS_CustomList);
225  }
226 
244  void MoveTo(const Vector2& newPosition, float duration, bool smooth=false, String onCompletionMessage="");
245 
257  void RotateTo(float newRotation, float duration, bool smooth=false, String onCompletionMessage="");
258 
270  void ChangeColorTo(const Color& newColor, float duration, bool smooth=false, String onCompletionMessage="");
271 
284  void ChangeSizeTo(const Vector2& newSize, float duration, bool smooth=false, String onCompletionMessage="");
285 
297  void ChangeSizeTo(float newSize, float duration, bool smooth=false, String onCompletionMessage="");
298 
307  int GetSpriteTexture(int frame = 0) const;
308 
327  bool SetSprite(const String& filename, int frame = 0, GLint clampmode = GL_CLAMP, GLint filtermode = GL_LINEAR, bool optional=false);
328 
332  void ClearSpriteInfo();
333 
351  void LoadSpriteFrames(const String& firstFilename, GLint clampmode = GL_CLAMP, GLint filtermode = GL_LINEAR);
352 
353  //rb - TODO - Add a way to associate anim type, and frame indices to a name.
365  void PlaySpriteAnimation(float delay, spriteAnimationType animType = SAT_Loop, int startFrame = -1, int endFrame = -1, const char* animName = NULL);
366 
374  void SetSpriteFrame(int frame);
375 
381  int GetSpriteFrame() const { return _spriteCurrentFrame; }
382 
388  const bool IsSpriteAnimPlaying() const
389  {
390  return (_spriteFrameDelay > 0);
391  }
392 
401  virtual void AnimCallback(String animName) {}
402 
410  void SetUVs(const Vector2& lowleft, const Vector2& upright);
411 
420  void GetUVs(Vector2 &lowleft, Vector2 &upright) const;
421 
429  const bool IsTagged(const String& tag);
430 
438  void Tag(const String& newTag);
439 
447  void Untag(const String& oldTag);
448 
455  const StringSet& GetTags() const;
456 
466  const String& SetName(String newName);
467 
473  const String& GetName() const;
474 
482  static Actor* const GetNamed(const String& nameLookup);
483 
494  virtual void ReceiveMessage(Message* message) {}
495 
505  void SetLayer(int layerIndex);
506 
517  void SetLayer(const String& layerName);
518 
528  virtual void Update(float dt);
529 
538  virtual void Render();
539 
544  virtual void LevelUnloaded() {}
545 
561  {
562  return this;
563  }
564 
588  static Actor* Create(const String& archetype);
589 
598  static void SetScriptCreatedActor(Actor* a) { _scriptCreatedActor = a; }
599 
606  virtual const String GetClassName() const { return "Actor"; }
607 
608 protected:
609  Vector2 _size;
610  Vector2 _position;
611  Color _color;
612  float _rotation;
613 
614  float _UV[8];
615 
616  actorDrawShape _drawShape;
617  static const float _squareVertices[];
618  static float _circleVertices[];
619  static float _circleTextureCoords[];
620 
621  int _spriteCurrentFrame;
622  int _spriteNumFrames;
623  float _spriteFrameDelay;
624  float _spriteCurrentFrameDelay;
625  int _spriteTextureReferences[MAX_SPRITE_FRAMES];
626  spriteAnimationType _spriteAnimType;
627  int _spriteAnimStartFrame;
628  int _spriteAnimEndFrame;
629  int _spriteAnimDirection;
630  int _displayListIndex;
631 
632  StringSet _tags;
633 
634  String _name;
635  static std::map<String, Actor*> _nameList;
636 
637  String _currentAnimName;
638  static Actor* _scriptCreatedActor;
639 
640 private:
641  void SetSpriteTexture(int texRef, int frame = 0);
642  void UpdateSpriteAnimation(float dt);
643 
644  Interval<Vector2> _positionInterval; String _positionIntervalMessage;
645  Interval<float> _rotationInterval; String _rotationIntervalMessage;
646  Interval<Color> _colorInterval; String _colorIntervalMessage;
647  Interval<Vector2> _sizeInterval; String _sizeIntervalMessage;
648 };
649 
650 typedef std::vector<Actor*> ActorList;
651 typedef std::set<Actor*> ActorSet;
652