aboutsummaryrefslogtreecommitdiffstats
path: root/src/draft/draft.pde
blob: 0852cb58300596b621c68ef0c806a70016e5c3a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <SPI.h>
#include <GD.h>

#include "allsewage.h" // http://gameduino.com/results/b2f4c588/
#include "sprites.h"   // http://gameduino.com/results/16fe50de/

#define SWIM_SPEED    1

#define WEAPON_GROUP  0
#define TARGET_GROUP  1

#define NUM_TRASH         20

#define KEY_UP(x)      ((x >> 7) & 0x01)
#define KEY_DOWN(x)    ((x >> 6) & 0x01)
#define KEY_LEFT(x)    ((x >> 5) & 0x01)
#define KEY_RIGHT(x)   ((x >> 4) & 0x01)
#define KEY_C(x)       ((x >> 3) & 0x01)
#define KEY_V(x)       ((x >> 2) & 0x01)
#define KEY_SPACE(x)   ((x >> 1) & 0x01)
#define KEY_ESCAPE(x)  ((x >> 0) & 0x01)

static boolean last_key_space = false;  // last keypress a spacebar?
static unsigned int t = 0;  // global game counter

static uint16 swimx, swimy;  // swimmer position
static int8 swimvx, swimvy;  // velocity
static int8 weapon_sprid;
static uint8 jaws_closed;      // swimmer weapon state

static uint16 narcox, narcoy;
static int8 narcovx, narcovy;
static uint8 narco_sprid;
static boolean narco_sunk;
static uint8 enemy_sprid;


static unsigned int scrollx;  // background scroll
static unsigned int scrolly;
static int8 scrollvx; // background scroll speed (128 is one pixel/frame)

static uint16 trash_rate;
static unsigned char inchar;
static uint8 touching_weapon, touching_enemy;

void static process_keypress() {
  if(SerialUSB.available()) {
    inchar = SerialUSB.read();
    while(SerialUSB.available()) {
      inchar = SerialUSB.read();
    }
    //SerialUSB.println(inchar, 2);
    swimvx = 0;
    swimvy = 0;
    if (KEY_LEFT(inchar)) { swimvx -= SWIM_SPEED; }
    if (KEY_RIGHT(inchar)) { swimvx += SWIM_SPEED; }
    if (KEY_UP(inchar)) { swimvy -= SWIM_SPEED; }
    if (KEY_DOWN(inchar)) { swimvy += SWIM_SPEED; }
    if (KEY_SPACE(inchar) and !last_key_space) { jaws_closed = 20; }
    if (!KEY_SPACE(inchar)) { jaws_closed = 0; }
    last_key_space = KEY_SPACE(inchar);
    if (KEY_ESCAPE(inchar)) {
      clear_all_sprites();
      narco_sunk = false;
      narcoy = 150;
    }
  }
}

boolean static wait_screen() {
  if(SerialUSB.available()) {
    inchar = SerialUSB.read();
    while(SerialUSB.available()) {
      inchar = SerialUSB.read();
    }

    if (KEY_SPACE(inchar)) { return true; }
    if (KEY_ESCAPE(inchar)) { return true; }
    return false;
  }
}

void setup()
{
  pinMode(BOARD_BUTTON_PIN, INPUT);
  swimx = 20;
  swimy = 20;
  narcox = 180;
  narcoy = 150;
  narcovy = -1;
  trash_rate = 100;
  
  delay(250);
  GD.begin();
  
  GD.wr(JK_MODE, 1);
  
  init_sprites();  // called twice
  GD.waitvblank();
  init_splashpage();
  
  while(!wait_screen()) {
    delay(100);
  }
  clear_all_sprites();
  GD.waitvblank();
  init_background();
  init_sprites();
  init_trash();
  
  add_trash();
  add_trash();
  add_trash();
}

void loop() 
{
  GD.waitvblank();
  
  // ------- do graphics drawing
  
  // check collisions
  touching_weapon = GD.rd(COLLISION + weapon_sprid);
  touching_enemy = GD.rd(COLLISION + enemy_sprid);
  
  // scroll background
  GD.wr16(SCROLL_X, scrollx);
  GD.__wstartspr(0);
  //draw_standing_all(10,270);
  draw_all_trash();
  if (t > 72 * 30) {
    draw_narco(narcox, narcoy, narco_sunk);
    if (narco_sunk) {
      for (uint8 i = 0; i < 5; i++) {
        draw_explosion(narcox+random(96)-8, narcoy+random(64)-8);
      }
    }
  }
  draw_swim(swimx,swimy, (jaws_closed == 0));
  GD.__end();
  
  // -------- do logic stuff
  scrollx += ((t % scrollvx) == 0);
  //swimx = (swimx + 1) % 400;
  if(jaws_closed > 0) {
    jaws_closed -= 1;
  }
  
  update_trash();
  cleanup_trash();
  
  process_keypress();
 
  if (jaws_closed > 15 and (touching_weapon == enemy_sprid || touching_enemy == weapon_sprid)) {
    narco_sunk = true;
  }
  
  if (jaws_closed > 15 and (touching_weapon != 255)) {
    check_trash(touching_weapon);
  }
  
  swimx = (swimx + swimvx);
  swimy = (swimy + swimvy);
  if (swimx > 380) swimx = 380;
  if (swimx < 32) swimx = 32;
  if (swimy > 250) swimy = 250;
  if (swimy < 50) swimy = 50;

  if(!narco_sunk) {
    if(narcoy > 170) { narcovy = -1; }
    if(narcoy < 130) { narcovy = 1; }
  } else {
    narcovy = 1;
    if(narcoy > 400) { narcovy = 0; }
  }
  if ((t % trash_rate) == 0 && (random(3) == 1)) {
    add_trash();
  }
  
  scrollvx = get_stuck() + 2;
  
  narcoy += narcovy;
  t += 1;
}