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