Changeset 23
- Timestamp:
- 12/11/07 05:19:20 (1 year ago)
- Files:
-
- trunk/guisterax/Makefile (modified) (1 diff)
- trunk/guisterax/src/actor.d (modified) (9 diffs)
- trunk/guisterax/src/element.d (modified) (6 diffs)
- trunk/guisterax/src/game.d (modified) (1 diff)
- trunk/guisterax/src/surface.d (modified) (2 diffs)
- trunk/guisterax/src/timer.d (modified) (2 diffs)
- trunk/guisterax/src/universe.d (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/guisterax/Makefile
r20 r23 13 13 gdc -fdebug -g -o guisterax src/*.d src/derelict/util/*.d src/derelict/sdl/*.d -Isrc/ -Isrc/derelict -lm 14 14 15 dmd: src/*.d 16 dmd -op -ofguisterax src/*.d src/derelict/util/*.d src/derelict/sdl/*.d -Isrc/ -Isrc/derelict -L-ldl 17 15 18 clean: 16 rm -rf build guisterax *.o *.bp *.greg 19 rm -f guisterax 20 find -name '*.o' | xargs rm -f trunk/guisterax/src/actor.d
r10 r23 28 28 import display; 29 29 30 /** 31 Base class for everything in the game. 32 33 An actor has a parent actor and a list of children actor, so that all the actors of the game are in a tree-like structure. 34 Every actors have an iter, move and update methods. They are called in that order at each iteration of the game. 35 */ 30 36 class Actor 31 37 { … … 35 41 36 42 public: 43 /// The parent of this actor. 37 44 Actor parent; 45 /// All the children of this actor. 38 46 Actor[] children; 47 /// Defines if the actor has to be deleted or not. 39 48 bool alive = true; 40 49 41 50 public: 51 /// Return all the children that are of type T. 42 52 T[] children_as(T)() { 43 53 bool is_t(in Actor a) {return cast(T)a !is null;} … … 45 55 } 46 56 57 /// Return the first ancestor of type T. 47 58 T parent_as(T)() { 48 59 T casted = cast(T)parent; … … 52 63 } 53 64 65 /// Return the fist ancestor of type Universe. 54 66 Universe universe() {return parent_as!(Universe)();} 55 67 68 /// Kill the actor (the parent will delete it in the next iteration step). 56 69 void kill() {alive = false;} 57 70 58 71 this() {} 59 72 73 /// Indicate how much the actor contribute to the global danger of the game. 60 74 float danger() {return 0;} 61 75 76 /** Append a children actor. 77 Params: 78 a = the actor to add 79 end = add the actor at the end of the children list 80 */ 62 81 void append(Actor a, bool end = true) { 63 82 if (end) { … … 68 87 a.parent = this; 69 88 } 89 90 /** Append a children actor. 91 Params: 92 a = the actor to add 93 pos = add the actor just before this 94 */ 70 95 void append(Actor a, Actor pos) { 71 96 m_next_children = insert(m_next_children, a, pos); … … 73 98 } 74 99 75 void iter(float dt = 1) { 100 /// Iter the actor of a given time 101 void iter(float dt) { 76 102 foreach(a; children) { 77 103 a.iter(dt); … … 79 105 } 80 106 81 void move(float dt = 1) { 107 /// Move the actor of a given time 108 void move(float dt) { 82 109 foreach(a; children) { 83 110 a.move(dt); … … 85 112 } 86 113 114 /// Update the actor 87 115 void update() { 88 116 // update actor list … … 95 123 } 96 124 125 /** 126 Draw the actor on a display 127 Params: 128 disp = the display to draw on 129 pos = the pos where to draw 130 */ 97 131 void draw(Display disp, in Vect pos = Vect()) { 98 132 foreach(a; children) { trunk/guisterax/src/element.d
r22 r23 36 36 import std.stdio; 37 37 38 /** 39 Special Actor that represents a circular object in a Universe 40 41 Elements have some additional attributes, like pos, speed, radius... 42 */ 38 43 class Element : Actor 39 44 { … … 42 47 43 48 public: 44 bool stay_inside_universe = true; 45 Vect pos = Vect(0,0); 46 Vect speed = Vect(0,0); 47 Vect force; 48 float mass = 1; 49 Sprite sprite; 50 float max_speed = float.infinity; 51 float radius = 0; 52 float friction = 0; 49 Vect pos = Vect(); /// The pos of the element in the universe 50 Vect speed = Vect(); /// The speed of the element in the universe 51 Vect force; /// The force currently applied on the element 52 float mass = 1; /// The mass of the element 53 Sprite sprite; /// The sprite 54 float max_speed = float.infinity; /// The element can't go faster than that 55 float radius = 0; /// Used to compute the collisions 56 float friction = 0; /// Used to slow down the element 53 57 54 58 public: 55 59 /** 60 Set the angle of the element 61 62 It also set the sprite image, because the sprite images represent the element under angles from 0 to 2 * PI 63 */ 56 64 void angle(float a) { 57 65 m_angle = a; … … 61 69 float angle() {return m_angle;} 62 70 71 /// Deprecated: use e.force += x instead 63 72 void add_force(in Vect f) {force += f;} 64 73 74 /// Use if the element can be hitten (like the ship for example) 65 75 void hit(float v) {} 66 76 77 /** 78 Constructor 79 Params: 80 sprite = every image of the sprite represent a given angle 81 pos = the initial position of the element 82 */ 67 83 this(Sprite sprite = null, in Vect pos = Vect()) { 68 84 super(); … … 71 87 } 72 88 89 /** 90 Add an attraction force in a given direction 91 Params: 92 pos = the direction of attraction 93 strenght = the strenght of the force 94 direct = define if the force should be applied proportionally to the time or not 95 if direct = true, the force is directly added to the speed 96 */ 73 97 void attract(in Vect pos, float strenght, bool direct = true) { 74 98 Vect normal = (pos - this.pos).normal(); … … 79 103 } 80 104 } 105 /// ditto 81 106 void repeal(in Vect pos, float strenght, bool direct = true) { 82 107 attract(pos, -strenght, direct); … … 84 109 85 110 86 111 /// Return true if the element can collide into an other given element 87 112 bool can_collide(in Element e) {return false;} 113 /// Compute the result of the collision with an other element 88 114 void collide(Element e) {} 89 115 trunk/guisterax/src/game.d
r17 r23 110 110 } 111 111 override void iter(float dt) { 112 super.iter( );112 super.iter(dt); 113 113 if (pressed_key(SDLK_RETURN)) { 114 114 kill(); trunk/guisterax/src/surface.d
r16 r23 32 32 public import display; 33 33 34 /** 35 Represent a drawable surface. 36 */ 34 37 class Surface 35 38 { … … 69 72 }; 70 73 74 /** 75 Represent a rectangular part of a surface 76 */ 71 77 class SubSurface : Surface 72 78 { trunk/guisterax/src/timer.d
r10 r23 26 26 import utils; 27 27 28 /** 29 Trigger a function after a given time 30 */ 28 31 class Timer: Actor 29 32 { … … 55 58 }; 56 59 60 /** 61 Trigger a function after a given random time 62 */ 57 63 class RandTimer : Timer 58 64 { 59 65 float p; 66 /** 67 Constructor 68 Params: 69 e = probability that the event append in a unity time 70 func = function to call 71 loop = if true reinit the timer after each events 72 */ 60 73 this(float e, void delegate() func, bool loop = false){ 61 74 p = 1/e; trunk/guisterax/src/universe.d
r21 r23 32 32 import surface; 33 33 34 /** 35 Special Display that display the element several time if they touch the universe border 36 */ 34 37 class UniverseDisplay : Display 35 38 { … … 61 64 }; 62 65 66 /** 67 Special Actor in which the element children can collide into each other 68 */ 63 69 class Universe : Actor 64 70 { … … 79 85 float bottom() {return -m_height / 2;} 80 86 87 /// Return all the elements in the universe 81 88 Element[] elements() {return m_elements;} 82 89 … … 86 93 } 87 94 88 override void iter(float dt = 1) {89 // /Get the keys95 override void iter(float dt) { 96 // Get the keys 90 97 bool[] n_keys = MainDisplay().getKeys(); 91 98 m_pressed_keys.length = m_keys.length; … … 113 120 } 114 121 122 /// Check if a key is currently down. 115 123 bool key(in SDLKey key) { 116 124 if (key >= m_keys.length) {return false;} … … 118 126 } 119 127 128 /// Check if a key has been pressed. 120 129 bool pressed_key(in SDLKey key) { 121 130 if (key >= m_pressed_keys.length) {return false;} … … 123 132 } 124 133 134 /// Check if two elements collide. 125 135 static bool collide(in Element e1, in Element e2) { 126 136 return (e1.pos - e2.pos).norm <= e1.radius + e2.radius; 127 137 } 128 138 139 /// Compute the all collisions 129 140 void compute_collisions() { 130 141 for(int i = 0; i < m_elements.length; ++i) {
