1 module example.gameentity; 2 3 version(GAMEEXAMPLE){ 4 5 import nudsfml.graphics; 6 7 import std.stdio; 8 9 import gamemap; 10 11 class Entity { 12 Vector2f position; 13 Vector2i mapLocation; 14 int id; 15 bool destroy=false; 16 17 this(){} 18 void update(float dt){} 19 void draw(RenderTarget target){} 20 } 21 22 23 class SnakeEntity : Entity { 24 int partsID; 25 int parentID; 26 int childID; 27 28 RectangleShape shape; 29 30 Texture texture; 31 32 this (Texture t){ 33 texture=t; 34 shape = new RectangleShape(); 35 shape.size(Vector2f(32,48)); 36 shape.fillColor(Color.White); 37 shape.setTexture(texture); 38 shape.textureRect = IntRect(6*32,2*48,32,48); 39 } 40 41 override void update(float dt){ 42 if(destroy){ 43 return; 44 } 45 } 46 47 override void draw(RenderTarget target){ 48 position = Vector2f(mapLocation.x * 32, mapLocation.y * 32); 49 shape.position = position; 50 target.draw(shape); 51 } 52 } 53 54 55 class AppleEntity : Entity { 56 Vector2f offset = Vector2f(0, -8); 57 58 float floatTime = 0; 59 float floatTimeMultiplyer = 1.0; 60 61 Texture texture; 62 RectangleShape shape; 63 RectangleShape shadow; 64 65 this(Texture t) { 66 texture = t; 67 shape = new RectangleShape(); 68 shape.setTexture(t); 69 //TODO!!! - load tile texture position and size from file 70 shape.size(Vector2f(32, 48)); 71 shape.textureRect = IntRect(32 * 4, 48*2, 32, 48); 72 shape.fillColor = Color.White; 73 74 shadow = new RectangleShape(); 75 shadow.setTexture(t); 76 //TODO!!! - load tile texture position and size from file 77 shadow.size(Vector2f(32, 48)); 78 shadow.textureRect = IntRect(32 * 5, 48*2, 32, 48); 79 shadow.fillColor = Color.White; 80 81 super(); 82 } 83 84 override void update(float deltaTime){ 85 86 floatTime += deltaTime * floatTimeMultiplyer; 87 //writeln("deltaTime: ", deltaTime ," floatTime: ", floatTime, " multiplier: ", floatTimeMultiplyer); 88 if(floatTime > .75 ){ 89 floatTime = .75; 90 floatTimeMultiplyer = -floatTimeMultiplyer; 91 } 92 if(floatTime < 0 ){ 93 floatTime = 0.0; 94 floatTimeMultiplyer = -floatTimeMultiplyer; 95 } 96 97 super.update(deltaTime); 98 } 99 100 override void draw(RenderTarget target){ 101 shadow.position = position; 102 target.draw(shadow); 103 104 shape.position = position + offset * (floatTime/0.75); 105 target.draw(shape); 106 107 super.draw(target); 108 } 109 } 110 }