DUDE.CPP
上传用户:abcdshs
上传日期:2007-01-07
资源大小:1858k
文件大小:11k
源码类别:

游戏

开发平台:

Visual C++

  1. // (C) Copyright 1996 by Anthony J. Carin.  All Rights Reserved.
  2. #include <stdafx.h>
  3. #include <dude.h>
  4. #include <mmsystem.h>
  5. #include "levels.h"
  6. #include "antagon.h"
  7. #include "weapon.h"
  8. dude::dude(personal *p) : baseobject(p->body())
  9. {
  10.     setpersonal(*p);
  11.     float hight = p->hight();
  12.     hightlevel = 1.5f * hight;
  13.     if (m_body)
  14.         m_body->setyoff((direction)0);
  15.     coordinate left(0.3f*hight,0.14f*hight,0.0f);
  16.     coordinate right(-0.3f*hight,0.14f*hight,0.0f);
  17.     leftarm          = new totalarm(left,p);
  18.     rightarm         = new totalarm(right,p);
  19.     left.setto(0.1f*hight,-0.7f*hight,0.0f);
  20.     right.setto(-0.1f*hight,-0.7f*hight,0.0f);
  21.     leftleg          = new totalleg(left,p);
  22.     rightleg         = new totalleg(right,p);
  23.     left.setto(0.0f,0.5f*hight,-0.08f*hight);
  24.     neck             = new attachment(left);
  25.     m_head           = new head(p);
  26.     neck->attachto(m_head);
  27.     shirtcolor(RGB(0,148,0));
  28.     pantscolor(RGB(0,0,200));
  29.     skincolor(RGB(240,200,0));
  30.     m_health = 100;
  31.     rightweapon = leftweapon = NULL;
  32.     personaleffect();
  33. }
  34. dude::~dude()
  35. {
  36.     delete rightweapon;
  37.     delete leftweapon;
  38.     delete leftarm;
  39.     delete rightarm;
  40.     delete leftleg;
  41.     delete rightleg;
  42.     delete neck;
  43.     delete m_head;
  44. }
  45. void dude::shirtcolor(COLORREF c)
  46. {
  47.     setcolor(c);
  48.     leftarm->setcolor(c);
  49.     rightarm->setcolor(c);
  50. }
  51. void dude::pantscolor(COLORREF c)
  52. {
  53.     leftleg->setcolor(c);
  54.     rightleg->setcolor(c);
  55. }
  56. void dude::skincolor(COLORREF c)
  57. {
  58.     m_head->setcolor(c);
  59. }
  60. void dude::setto(view& v)
  61. {
  62.     baseobject::setto(v);
  63.     leftarm->setto(v);
  64.     rightarm->setto(v);
  65.     leftleg->setto(v);
  66.     rightleg->setto(v);
  67.     neck->setto(v);
  68. }
  69. void dude::draw()
  70. {
  71.     baseobject::draw();
  72.     leftarm->setto(m_view);
  73.     rightarm->setto(m_view);
  74.     leftleg->setto(m_view);
  75.     rightleg->setto(m_view);
  76.     neck->setto(m_view);
  77.     leftarm->draw();
  78.     rightarm->draw();
  79.     leftleg->draw();
  80.     rightleg->draw();
  81.     neck->draw();
  82.     float nhight;
  83.     float xnhight = tccos(m_view.xdirection(), hightlevel);
  84.     float znhight = tccos(m_view.zdirection(), hightlevel);
  85.     nhight = (znhight < xnhight) ? znhight : xnhight;
  86.     settledown(nhight);
  87.     view tmp;
  88.     if (rightweapon)
  89.     {
  90.        tmp = righthandloc();
  91.        tmp.setydir(tmp.ydirection()+(direction)TC_PI_2+rightweapontilt);
  92.        rightweapon->setto(tmp);
  93.        rightweapon->draw();
  94.     }
  95.     if (leftweapon)
  96.     {
  97.        tmp = lefthandloc();
  98.        tmp.setydir(tmp.ydirection()+(direction)TC_PI_2+leftweapontilt);
  99.        leftweapon->setto(tmp);
  100.        leftweapon->draw();
  101.     }
  102. }
  103. void dude::followcommands(command& c)
  104. {
  105.    CString tmpbuf;
  106.    LPCTSTR tmpptr;
  107.    int     tmpi;
  108.    instruction inst;
  109.    inst = c.next();
  110.    while (inst() != NOMORE && inst() != ENDSESSION)
  111.    {
  112.       switch (inst())
  113.       {
  114.       case RARMRAISEFWD:
  115.          rightarm->raisefoward(inst.value());
  116.          break;
  117.       case LARMRAISEFWD:
  118.          leftarm->raisefoward(inst.value());
  119.          break;
  120.       case RARMRAISEBCK:
  121.          rightarm->raisebackward(inst.value());
  122.          break;
  123.       case LARMRAISEBCK:
  124.          leftarm->raisebackward(inst.value());
  125.          break;
  126.       case RARMRAISESIDE:
  127.          rightarm->raisesideward(-inst.value());
  128.          break;
  129.       case LARMRAISESIDE:
  130.          leftarm->raisesideward(inst.value());
  131.          break;
  132.       case RARMBENDELBOW:
  133.          rightarm->bendelbow(inst.value());
  134.          break;
  135.       case LARMBENDELBOW:
  136.          leftarm->bendelbow(inst.value());
  137.          break;
  138.       case RLEGBENDKNEE:
  139.          rightleg->bendknee(inst.value());
  140.          break;
  141.       case LLEGBENDKNEE:
  142.          leftleg->bendknee(inst.value());
  143.          break;
  144.       case RLEGRAISEFWD:
  145.          rightleg->raisefoward(inst.value());
  146.          break;
  147.       case LLEGRAISEFWD:
  148.          leftleg->raisefoward(inst.value());
  149.          break;
  150.       case RLEGRAISEBCK:
  151.          rightleg->raisebackward(inst.value());
  152.          break;
  153.       case LLEGRAISEBCK:
  154.          leftleg->raisebackward(inst.value());
  155.          break;
  156.       case RLEGRAISESIDE:
  157.          rightleg->raisesideward(-inst.value());
  158.          break;
  159.       case LLEGRAISESIDE:
  160.          leftleg->raisesideward(inst.value());
  161.          break;
  162.       case TILTBODYRIGHT:
  163.          tiltright(inst.value());
  164.          break;
  165.       case TILTBODYLEFT:
  166.          tiltleft(inst.value());
  167.          break;
  168.       case TILTBODYFWD:
  169.          tiltfoward(inst.value());
  170.          break;
  171.       case TILTBODYBCK:
  172.          tiltbackward(inst.value());
  173.          break;
  174.       case TURNHEADLEFT:
  175.          m_head->turnleft(inst.value());
  176.          break;
  177.       case TURNHEADRIGHT:
  178.          m_head->turnright(inst.value());
  179.          break;
  180.       case TURNHEADUP:
  181.          m_head->turnup(inst.value());
  182.          break;
  183.       case TURNHEADDOWN:
  184.          m_head->turndown(inst.value());
  185.          break;
  186.       case CLEAR:
  187.          clear();
  188.          break;
  189.       case TURNRIGHT:
  190.          turnright(inst.value());
  191.          break;
  192.       case TURNLEFT:
  193.          turnleft(inst.value());
  194.          break;
  195.       case MOVEUPWARD:
  196.          moveupward(inst.value());
  197.          break;
  198.       case MOVEFOWARD:
  199.          movefoward(inst.value());
  200.          break;
  201.       case MOVEBACKWARD:
  202.          movebackward(inst.value());
  203.          break;
  204.       case PLAYSOUND:
  205.          tmpbuf = inst.string();
  206.          tmpbuf += ".wav";
  207.          PlaySound(getpath(tmpbuf), NULL, SND_ASYNC);
  208.          break;
  209.       case TILTRIGHTWEAPON:
  210.          rightweapontilt = (direction) (TC_PI2*inst.value());
  211.          break;
  212.       case TILTLEFTWEAPON:
  213.          leftweapontilt = (direction) (TC_PI2*inst.value());
  214.          break;
  215.       case PLAYGENERIC:
  216.          tmpi = (int) (inst.value());
  217.          switch(tmpi)
  218.          {
  219.          case 1:  tmpptr = m_personal.hia(); break;
  220.          case 2:  tmpptr = m_personal.ugh(); break;
  221.          default: tmpptr = m_personal.laugh(); break;
  222.          }
  223.          PlaySound(tmpptr, NULL, SND_ASYNC);
  224.          break;
  225.       case STRIKETOP:
  226.       case STRIKEMED:
  227.       case STRIKEBOT:
  228.       case THEYDO:
  229.          checkifgothit(inst);
  230.          break;
  231.       case HURTME:
  232.          takedamage(inst.value());
  233.          break;
  234.       case RTHROW:
  235.          if (rightweapon)
  236.          {
  237.              weapon *tmp = (weapon *)rightweapon;
  238.              releaserightweapon();
  239.              tmp->Throw(location().ydirection(), inst.value());
  240.          }
  241.          break;
  242.       case LTHROW:
  243.          if (leftweapon)
  244.          {
  245.              weapon *tmp = (weapon *)leftweapon;
  246.              releaseleftweapon();
  247.              tmp->Throw(location().ydirection(), inst.value());
  248.          }
  249.          break;
  250.       case PICKUP:
  251.          if (!leftweapon || !rightweapon)
  252.              pickup();
  253.          break;
  254.       }
  255.       inst = c.next();
  256.    }
  257. }
  258. void dude::clear()
  259. {
  260.     rightarm->raisefoward(0.0f);
  261.     leftarm->raisefoward(0.0f);
  262.     rightarm->raisesideward(0.0f);
  263.     leftarm->raisesideward(0.0f);
  264.     rightarm->bendelbow(0.0f);
  265.     leftarm->bendelbow(0.0f);
  266.     rightleg->bendknee(0.0f);
  267.     leftleg->bendknee(0.0f);
  268.     rightleg->raisefoward(0.0f);
  269.     leftleg->raisefoward(0.0f);
  270.     rightleg->raisesideward(0.0f);
  271.     leftleg->raisesideward(0.0f);
  272.     tiltright(0.0f);
  273.     tiltbackward(0.0f);
  274.     m_head->turnleft(0.0f);
  275.     m_head->turnup(0.0f);
  276. }
  277. void dude::moveupward(float v)
  278. {
  279.    coordinate tmp = m_view;
  280.    tmp.sety(tmp.y() + 1.0f*v);
  281.    m_view.stepto(tmp,7.0f);
  282. }
  283. void dude::movefoward(float v)
  284. {
  285.    view tmp = m_view;
  286.    tmp.setx(tmp.x() + tcsin(tmp.ydirection(), v));
  287.    tmp.setz(tmp.z() + tccos(tmp.ydirection(), v));
  288.    m_view.stepto(tmp,7.0f);
  289. }
  290. void dude::movebackward(float v)
  291. {
  292.    view tmp = m_view;
  293.    tmp.setx(tmp.x() - tcsin(tmp.ydirection(), v));
  294.    tmp.setz(tmp.z() - tccos(tmp.ydirection(), v));
  295.    m_view.stepto(tmp,7.0f);
  296. }
  297. void dude::checkifgothit(instruction& inst)
  298. {
  299.    short HitBonus = 0;
  300.    if (rightweapon)
  301.    {
  302.        if (((handheld *)rightweapon)->IsAKnife())
  303.            HitBonus = 10;
  304.        else if (((handheld *)rightweapon)->IsASword())
  305.            HitBonus = 30;
  306.    }
  307.    else if (leftweapon)
  308.    {
  309.        if (((handheld *)leftweapon)->IsAKnife())
  310.            HitBonus = 10;
  311.        else if (((handheld *)leftweapon)->IsASword())
  312.            HitBonus = 30;
  313.    }
  314.    if (Game->LevelPtr()->usercontrol->location() != location())
  315.    {
  316.        if (inrange(Game->LevelPtr()->usercontrol->location()) && !Game->Motionless())
  317.            Game->LevelPtr()->usercontrol->gothit(inst, HitBonus);
  318.        return;
  319.    }
  320.    antagonist *target;
  321.    target = findtarget();
  322.    if (target)
  323.        target->gothit(inst, HitBonus);
  324. }
  325. antagonist * dude::findtarget()
  326. {
  327.     antagonist *atmp;
  328.     atmp = (antagonist *)Game->DrawQueue()->first(ANTAGONIST);
  329.     while (atmp)
  330.     {
  331.         if (inrange(atmp->location()) && atmp->location() != location())
  332.             return atmp;
  333.         atmp = (antagonist *)Game->DrawQueue()->next(ANTAGONIST);
  334.     }
  335.     return NULL;
  336. }
  337. char dude::inrange(view& c)
  338. {
  339.     float ftmp;
  340.     ftmp = m_view.distanceto(c);
  341.     if (ftmp > 1.0f)
  342.         return FALSE;
  343.     if (fabs(c.y() - m_view.y()) > 1.0f)
  344.         return FALSE;
  345.     direction tmp;
  346.     tmp = m_view.ydirectionto(c);
  347.     tmp = m_view.ydirection() - tmp;
  348.     if (tmp > (direction) TC_PI_4 &&
  349.         tmp < (direction) -TC_PI_4)
  350.         return FALSE;
  351.     return TRUE;
  352. }
  353. void dude::takedamage(float v)
  354. {
  355.     m_health = (short) ((float)m_health - 100.0f*v);
  356.     if (m_health < 40)
  357.     {
  358.         if (random(4) == 2)
  359.             releaseleftweapon();
  360.         else
  361.             releaserightweapon();
  362.     }
  363.     if (m_health < 0)
  364.     {
  365.         m_health = 0;
  366.         releaserightweapon();
  367.         releaseleftweapon();
  368.     }
  369. }
  370. void dude::personaleffect()
  371. {
  372.     skincolor(m_personal.skincolor());
  373.     haircolor(m_personal.haircolor());
  374.     leftarm->setskincolor(m_personal.skincolor());
  375.     rightarm->setskincolor(m_personal.skincolor());
  376.     m_head->facesurface(m_personal.face());
  377. }
  378. void dude::releaserightweapon()
  379. {
  380.     if (rightweapon)
  381.     {
  382.         Game->Landscapes()->AddLSEntry(rightweapon->location(), (CString)"WEAPON", ((weapon *)rightweapon)->NumType());
  383.         Game->DrawQueue()->add(rightweapon);
  384.         rightweapon = 0;
  385.     }
  386. }
  387. void dude::releaseleftweapon()
  388. {
  389.     if (leftweapon)
  390.     {
  391.         Game->Landscapes()->AddLSEntry(leftweapon->location(), (CString)"WEAPON", ((weapon *)leftweapon)->NumType());
  392.         Game->DrawQueue()->add(leftweapon);
  393.         leftweapon = 0;
  394.     }
  395. }
  396. void dude::pickup()
  397. {
  398.     if (leftweapon && rightweapon)
  399.         return;
  400.     handheld *atmp;
  401.     atmp = (handheld *)Game->DrawQueue()->first(HANDHELD);
  402.     while (atmp)
  403.     {
  404.         if (inrange(atmp->location()))
  405.         {
  406.             atmp->RemoveLSEntry();
  407.             atmp->detach();
  408.             if (!rightweapon)
  409.             {
  410.                 rightweapon = atmp;
  411.                 break;
  412.             }
  413.             if (!leftweapon)
  414.             {
  415.                 leftweapon = atmp;
  416.                 break;
  417.             }
  418.         }
  419.         atmp = (handheld *)Game->DrawQueue()->next(HANDHELD);
  420.     }
  421. }