1 module example.game; 2 3 version(GAMEEXAMPLE){ 4 5 import nudsfml.graphics; 6 import nudsfml.system; 7 import nudsfml.audio; 8 import nudsfml.network; 9 10 import std.stdio; 11 import std.format; 12 13 import gamemap; 14 import gameentity; 15 import snake; 16 17 class Game { 18 string dataDir; 19 bool running = true; 20 21 RenderWindow win; 22 23 GameMap map; 24 AppleEntity [] apples; 25 //SceneManager 26 //scriptingEngine 27 // 28 29 Snake snake; 30 bool doMove = true; 31 32 Font systemFont; 33 Text debugText; 34 35 bool doDrawDebug = false; 36 37 Clock gameTick; 38 39 Clock elementTimer; 40 41 this(string dataDir_ = "data/"){ 42 dataDir = dataDir_; 43 44 win = new RenderWindow(VideoMode(1024, 768), "NudSFML"); 45 //win.setVerticalSyncEnabled(true); // higher accuracy but more cpu time 46 //win.setFramerateLimit(60); // 60 fps max // TODO: make this configurable 47 48 systemFont = new Font(); 49 systemFont.loadFromFile(dataDir ~ "CamingoCode-Regular.ttf"); 50 51 debugText = new Text("Test", systemFont, 16); 52 doDrawDebug = true; 53 //create initial resources 54 55 map = new GameMap(); 56 apples ~= map.addApple(5,4); 57 58 snake = new Snake(this); 59 snake.headPosition = Vector2i(map.width / 2, map.height / 2); 60 snake.direction = Direction.Up; 61 snake.generate(3); 62 63 gameTick = new Clock(); 64 elementTimer = new Clock(); 65 } 66 67 68 void startup() { 69 win.setTitle("素晴らしい !"d); 70 71 } 72 73 float dt, drawtime, eventTimer, updateTimer; 74 75 void run(){ 76 while(running){ 77 dt = gameTick.restart().asSeconds(); 78 drawtime = elementTimer.restart().asSeconds(); 79 handleEvents(); 80 eventTimer= elementTimer.getElapsedTime.asSeconds(); 81 update(dt); 82 updateTimer = elementTimer.getElapsedTime.asSeconds(); 83 draw(); 84 } 85 } 86 87 void update(float dt){ 88 if(Keyboard.isKeyPressed(Keyboard.Key.Up)){ 89 if(snake.canDirection(Direction.Up)){ 90 snake.direction = Direction.Up; 91 } 92 } else if(Keyboard.isKeyPressed(Keyboard.Key.Right)){ 93 if(snake.canDirection(Direction.Right)){ 94 snake.direction = Direction.Right; 95 } 96 } else if(Keyboard.isKeyPressed(Keyboard.Key.Down)){ 97 if(snake.canDirection(Direction.Down)){ 98 snake.direction = Direction.Down; 99 } 100 } else if(Keyboard.isKeyPressed(Keyboard.Key.Left)){ 101 if(snake.canDirection(Direction.Left)){ 102 snake.direction = Direction.Left; 103 } 104 } 105 map.update(dt); 106 snake.update(dt); 107 } 108 109 void draw(){ 110 win.clear(); 111 112 //draw stuff here 113 map.draw(Vector2f(0,16), win); 114 115 drawDebug(); 116 win.display(); 117 } 118 119 void drawDebug(){ 120 if(doDrawDebug){ 121 debugText.position = Vector2f(32,32); 122 string text = format("FPS: %0.f , drawTime: %f , eventTime: %f , updateTime: %f", 1/dt, drawtime, eventTimer, updateTimer); 123 debugText.setString = text; 124 win.draw(debugText); 125 } 126 } 127 128 void handleEvents(){ 129 Event e; 130 while(win.pollEvent(e)){ 131 switch(e.type){ 132 case Event.Type.Closed: 133 running = false; 134 break; 135 case Event.Type.KeyPressed: 136 switch(e.key.code) { 137 case Keyboard.Key.Escape: 138 running = false; 139 break; 140 case Keyboard.Key.F9: 141 snake.addPart(); 142 break; 143 case Keyboard.Key.F10: 144 snake.move(); 145 break; 146 case Keyboard.Key.F12: 147 doDrawDebug = !doDrawDebug; 148 break; 149 case Keyboard.Key.P: 150 auto screenShot = win.capture(); 151 writeln("Saved screenshot to screenShot.png"); 152 screenShot.saveToFile("screenshot.png"); 153 break; 154 case Keyboard.Key.Space: 155 doMove = !doMove; 156 break; 157 default: break; 158 } 159 break; 160 default: break; 161 } 162 } 163 } 164 165 void shutdown(){ 166 //clean up resources 167 168 } 169 } 170 }