/+junk/invaders_vala

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/invaders_vala

« back to all changes in this revision

Viewing changes to src/Game.vala

  • Committer: Gustav Hartvigsson
  • Date: 2012-10-09 21:59:24 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20121009215924-bsntg7a4ocaxkhoh
Implimented Time based movement...

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
using SDL;
2
2
using SDLGraphics;
3
3
using Gee;
4
 
using GameHelper;
 
4
//using GameHelper;
5
5
 
6
6
namespace invadersGame{
7
7
  
8
 
  private const int SCREEN_HEIGHT = 640;
9
 
  private const int SCREEN_WIDTH = 480;
10
 
  private const int SCREEN_FPS = 60;
 
8
  private const int16 SCREEN_HEIGHT = 640;
 
9
  private const int16 SCREEN_WIDTH = 480;
 
10
  private const int SCREEN_FPS = 120;
11
11
  private const int SCREEN_BPP = 32;
12
 
  
13
 
  class Game : Object{
14
 
    private ArrayList GameObjectList = new ArrayList<GameObject>();
 
12
  /**
 
13
   * The main class for the game.
 
14
   */
 
15
  public class Game : Object{
 
16
    
 
17
    private SDL.Rect my_fill_rect;
 
18
    private MovementDelta my_movement_delta;
 
19
    
15
20
    /**GameObjectList
16
21
     * Stores the objects
17
22
     */
18
 
     
19
 
    private GameObject testObject;
 
23
    private ArrayList<GameObject> GameObjectList = new ArrayList<GameObject>();
20
24
    
21
25
    private uint32 fpsTimer;
22
26
    
25
29
    private bool eventBool[322];
26
30
    private bool isRun;
27
31
    
 
32
    private GameObjectPlayer player;
 
33
    
 
34
    /**getRandom
 
35
     * Returns a value between 0 and i.
 
36
     * (This is probobly not the most efficient to do this).
 
37
     */
28
38
    public static int getRandom(int i){
29
 
      /**getRandom
30
 
       * Returns a value between 0 and i.
31
 
       * (This is probobly not the most efficient to do this).
32
 
       */
 
39
      
33
40
      GLib.Rand rand = new GLib.Rand();
34
41
      return rand.int_range(0, i);
35
42
      //return (int) rand.next_int() % i;
36
43
    } //getRandom
37
44
    
38
45
    public Game(){
 
46
      
 
47
      ResourceHandler.init();
 
48
      
 
49
      player = new GameObjectPlayer();
 
50
      
 
51
      for(int16 i = 0; i < 10; i++) {
 
52
        stdout.printf("Adding object!\n");
 
53
        GameObjectList.add(new GameObject(i*16, i*16, 0, 0,0, 0));
 
54
      }
 
55
      
 
56
      my_fill_rect.x = 0;
 
57
      my_fill_rect.y = 0;
 
58
      my_fill_rect.h = SCREEN_HEIGHT;
 
59
      my_fill_rect.w = SCREEN_WIDTH;
 
60
      
39
61
      SDL.init(SDL.InitFlag.EVERYTHING);
40
62
      stdout.printf("Running contructor...\n");
41
63
      this.isRun = true;/* Is the mainloop running? */
42
64
      //this.rand = new GLib.Rand();
43
65
      
44
 
      testObject = new GameObject();
45
 
      
46
66
      for(int i = 0; i < 322; i++) { // init them all to false
47
67
        eventBool[i] = false;
48
68
      }
51
71
    public void mainLoop(){
52
72
      initScreen();
53
73
      while(this.isRun){
 
74
        player.move();
 
75
        
54
76
        fpsTimer = SDL.Timer.get_ticks();
55
77
        draw();
56
78
        process_events();
57
79
        process_keys();
58
80
        
 
81
        player.set_movment_delta(my_movement_delta);
 
82
        
59
83
        if(SDL.Timer.get_ticks() - fpsTimer < 1000/SCREEN_FPS){
60
 
          SDL.Timer.delay(1000/SCREEN_FPS - (SDL.Timer.get_ticks() - fpsTimer));
 
84
            SDL.Timer.delay(1000/SCREEN_FPS - (SDL.Timer.get_ticks() - fpsTimer));
61
85
        }
 
86
        
62
87
      }
63
88
    }//mainLoop
64
89
    
75
100
    } //initScreen
76
101
    
77
102
    private void draw(){
78
 
      //TODO
 
103
      /* Draw the player! */
 
104
      player.draw(screen);
 
105
      
 
106
      /* Draw all the items in the list! */
 
107
      foreach(var g in GameObjectList) {
 
108
        g.draw(screen);
 
109
      }
 
110
      
79
111
      this.screen.flip();
 
112
      this.screen.fill(my_fill_rect, 0x0000000);
80
113
    } // draw
81
114
    
82
115
    private void process_events(){
99
132
    } // process_events
100
133
    
101
134
    private void process_keys(){
 
135
      my_movement_delta = {0,0};
 
136
       
102
137
      if(eventBool[KeySymbol.ESCAPE] || eventBool[KeySymbol.q]){
103
138
        stdout.printf("derp!\n");
104
139
        this.isRun = false;
105
140
      }
 
141
      
 
142
      
 
143
      if (eventBool[KeySymbol.DOWN] && eventBool[KeySymbol.UP]) {
 
144
        
 
145
      } else if (eventBool[KeySymbol.UP]){
 
146
        my_movement_delta.y = -1;
 
147
      } else if (eventBool[KeySymbol.DOWN]){
 
148
        my_movement_delta.y = 1;
 
149
      }
 
150
      
 
151
      if (eventBool[KeySymbol.RIGHT] && eventBool[KeySymbol.LEFT]){
 
152
        
 
153
      } else if (eventBool[KeySymbol.LEFT]){
 
154
        my_movement_delta.x = -1;
 
155
      } else if (eventBool[KeySymbol.RIGHT]){
 
156
        my_movement_delta.x = 1;
 
157
      }
 
158
      
 
159
      
106
160
    } // process_keys
107
161
    /*----------------------------------------------------------------------*/
108
162
  }