00001 #include "objects.h"
00002
00003 void Cable::setup() {
00004 c1 = game.RAND(0, net.units-1);
00005 do {
00006 c2 = game.RAND(0, net.units-1);
00007 } while (c2 == c1);
00008 active = index = 0;
00009 delay = spark.delay(game.level);
00010 x1 = net.computers[c1].x + net.width/3;
00011 x2 = net.computers[c2].x + net.width/3;
00012 y1 = net.computers[c1].y + net.height/2;
00013 y2 = net.computers[c2].y + net.height/2;
00014 }
00015
00016 void Cable::update () {
00017 if (active) {
00018 if ((net.computers[c1].os == OS.WINGDOWS) ==
00019 (net.computers[c2].os == OS.WINGDOWS))
00020 active=0;
00021 else if (net.computers[c1].os == OS.WINGDOWS ||
00022 net.computers[c2].os == OS.WINGDOWS)
00023 {
00024 int dir, xdist, ydist,c;
00025 float sx, sy;
00026 dir = (net.computers[c2].os == OS.WINGDOWS);
00027 if (dir)
00028 {xdist=x1-x; ydist=y1-y;}
00029 else
00030 {xdist=x2-x; ydist=y2-y;}
00031 sx = xdist >= 0 ? 1.0 : -1.0;
00032 sy = ydist >= 0 ? 1.0 : -1.0;
00033 xdist = abs(xdist);
00034 ydist = abs(ydist);
00035 if (xdist==0 && ydist==0) {
00036 if (dir==0) c=c2; else c=c1;
00037 if (!net.computers[c].busy) {
00038 if (net.computers[c].os == OS.OFF)
00039 net.off--;
00040 else
00041 net.base--;
00042 net.win++;
00043 net.computers[c].os=OS.WINGDOWS;
00044 }
00045 active=0;
00046 }
00047 else if (game.MAX (xdist, ydist) < spark.speed) {
00048 if (dir)
00049 {x = x1; y = y1;}
00050 else
00051 {x = x2; y = y2;}
00052 }
00053 else {
00054 fx+=(xdist*spark.speed*sx)/(xdist+ydist);
00055 fy+=(ydist*spark.speed*sy)/(xdist+ydist);
00056 x = (int)fx;
00057 y = (int)fy;
00058 }
00059 index = 1 - index;
00060 }
00061 }
00062 else {
00063 if ((net.computers[c1].os == OS.WINGDOWS) ==
00064 (net.computers[c2].os == OS.WINGDOWS))
00065 delay = spark.delay(game.level);
00066 else if (net.computers[c1].os == OS.WINGDOWS ||
00067 net.computers[c2].os == OS.WINGDOWS)
00068 {
00069 if (delay>0) delay--;
00070 else {
00071 active = 1;
00072 if (net.computers[c1].os == OS.WINGDOWS)
00073 {fx=x=x1; fy=y=y1;}
00074 else
00075 {fx=x=x2; fy=y=y2;}
00076 }
00077 }
00078 }
00079 }
00080
00081 int Cable::onspark (int locx, int locy) {
00082 if (!active) return 0;
00083 return (abs(locx-x) < spark.width
00084 && abs(locy-y) < spark.height);
00085 }
00086
00087 void Cable::draw() {
00088 int rx = x - spark.width/2;
00089 int ry = y - spark.height/2;
00090 ui.draw_line(x1,y1,x2,y2);
00091 if (active)
00092 ui.draw(spark.pictures[index], rx, ry);
00093 }
00094