objects.c - gnuskii - GNUSki improved for ascii skiing experience.
 (HTM) git clone git://bitreich.org/gnuskii git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/gnuskii
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Tags
 (DIR) README
 (DIR) LICENSE
       ---
       objects.c (4031B)
       ---
            1 /*  GNUSki - a clone of the old game Skifree where you race downhill
            2     avoiding rocks and trees and try to score points by doing some
            3     tricks.
            4     Copyright (C) 2007 Rudolf Olah
            5 
            6     This program is free software; you can redistribute it and/or modify
            7     it under the terms of the GNU General Public License as published by
            8     the Free Software Foundation; either version 2 of the License, or
            9     (at your option) any later version.
           10 
           11     This program is distributed in the hope that it will be useful,
           12     but WITHOUT ANY WARRANTY; without even the implied warranty of
           13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
           14     GNU General Public License for more details.
           15 
           16     You should have received a copy of the GNU General Public License
           17     along with this program; if not, write to the Free Software
           18     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
           19 */
           20 
           21 #include "objects.h"
           22 
           23 void
           24 setupColors ()
           25 {
           26   if (has_colors () == false)
           27     {
           28       endwin ();
           29       printf ("*** Your terminal does not support color ***\n");
           30       exit (1);
           31     }
           32   start_color ();
           33   /* tree */
           34   init_pair (1, COLOR_GREEN, COLOR_BLACK);
           35   /* rock */
           36   init_pair (2, COLOR_WHITE, COLOR_BLACK);
           37   /* bigfoot */
           38   init_pair (3, COLOR_BLUE, COLOR_BLACK);
           39   /* snowman */
           40   init_pair (4, COLOR_CYAN, COLOR_BLACK);
           41 }
           42 
           43 struct Object
           44 makeObject (enum objectType type, int x, int y)
           45 {
           46   struct Object o;
           47   o.type = type;
           48   o.x = x;
           49   o.y = y;
           50   o.trick = 0;
           51   return o;
           52 }
           53 
           54 void
           55 setPosition (struct Object* o, int x, int y)
           56 {
           57   o->x = x;
           58   o->y = y;
           59 }
           60 
           61 void
           62 moveObject (struct Object* o, char facing, int speed)
           63 {
           64   switch (facing)
           65     {
           66     case 'n': o->y -= speed; break;
           67     case 'e': o->x += speed; break;
           68     case 's': o->y += speed; break;
           69     case 'w': o->x -= speed; break;
           70     case '1': o->y -= speed; o->x -= speed; break;
           71     case '2': o->y -= speed; o->x += speed; break;
           72     case '3': o->y += speed; o->x += speed; break;
           73     case '4': o->y += speed; o->x -= speed; break;
           74     }
           75 }
           76 
           77 void
           78 draw (struct Object o, char facing)
           79 {
           80   switch (o.type)
           81     {
           82     case skier:
           83       switch (facing)
           84         {
           85         case 'n':
           86         case 's':
           87           move (o.y, o.x);
           88           if (o.trick)
           89             printw ("@@");
           90           else
           91             printw ("||");
           92           break;
           93         case 'w':
           94           move (o.y, o.x);
           95           if (o.trick)
           96             printw ("@@");
           97           else
           98             printw ("==");
           99           break;
          100         case 'e':
          101           move (o.y, o.x+1);
          102           if (o.trick)
          103             printw ("@@");
          104           else
          105             printw ("==");
          106           break;
          107         case '1':
          108         case '3':
          109           move (o.y, o.x);
          110           if (o.trick)
          111             printw ("@@");
          112           else
          113             printw ("\\\\");
          114           break;
          115         case '2':
          116         case '4':
          117           move (o.y, o.x);
          118           if (o.trick)
          119             printw ("@@");
          120           else
          121             printw ("//");
          122           break;
          123         }
          124       break;
          125     case tree:
          126       attron (COLOR_PAIR (1));
          127       move (o.y, o.x);
          128       printw ("/|\\");
          129       move (o.y+1, o.x);
          130       printw ("/|\\");
          131       move (o.y+2, o.x);
          132       printw (" | ");
          133       attroff (COLOR_PAIR (1));
          134       break;
          135     case rock:
          136       attron (COLOR_PAIR (2));
          137       move (o.y, o.x);
          138       printw ("o");
          139       attroff (COLOR_PAIR (2));
          140       break;
          141     case hill:
          142       move (o.y, o.x);
          143       printw ("/^\\");
          144       break;
          145     case bigfoot:
          146       attron (COLOR_PAIR(3));
          147       move (o.y, o.x);
          148       printw (" O ");
          149       move (o.y+1, o.x);
          150       printw ("\\|/");
          151       move (o.y+2, o.x);
          152       printw ("/ \\");
          153       attroff (COLOR_PAIR(3));
          154       break;
          155     case snowman:
          156       attron (COLOR_PAIR(4));
          157       move (o.y, o.x);
          158       printw (" o ");
          159       move (o.y+1, o.x);
          160       printw ("\\O/");
          161       move (o.y+2, o.x);
          162       printw (" O ");
          163       attroff (COLOR_PAIR(4));
          164       break;
          165     case none: default:
          166       break;
          167     }
          168   move (0, 0);
          169 }
          170 
          171 int
          172 collision (struct Object player, struct Object target)
          173 {
          174   switch (target.type)
          175     {
          176     case tree:
          177     case bigfoot:
          178     case snowman:
          179       return player.y == target.y+2 && player.x == target.x+1;
          180       break;
          181     case rock:
          182       return player.y == target.y && player.x == target.x;
          183       break;
          184     case none: default:
          185       return 0;
          186     }
          187   return 0;
          188 }