improved physic
authorTobias Gläßer <tobi.web@gmx.de>
Thu, 19 Feb 2004 19:07:34 +0000 (19:07 +0000)
committerTobias Gläßer <tobi.web@gmx.de>
Thu, 19 Feb 2004 19:07:34 +0000 (19:07 +0000)
SVN-Revision: 130

src/badguy.c
src/physic.c
src/physic.h

index fe5b7e3..fa20790 100644 (file)
@@ -84,10 +84,10 @@ void badguy_action(bad_guy_type* pbad)
                     {
                       pbad->dir = RIGHT;
                     }
-                   else if (issolid( pbad->base.x + pbad->base.width-1, (int) pbad->base.y))
-                   {
+                  else if (issolid( pbad->base.x + pbad->base.width-1, (int) pbad->base.y))
+                    {
                       pbad->dir = LEFT;
-                   }
+                    }
                 }
 
               /* Fall if we get off the ground: */
@@ -172,26 +172,32 @@ void badguy_action(bad_guy_type* pbad)
 
               /* Bump into things horizontally: */
 
+              /* Bump into things horizontally: */
+
               if (!pbad->dying)
                 {
-                  if (issolid(pbad->base.x, pbad->base.y))
+                  int changed = pbad->dir;
+                  if (issolid( pbad->base.x - 1, (int) pbad->base.y))
                     {
-                      pbad->dir = !pbad->dir;
-
-                      if (pbad->mode == KICK)
-                        {
-                          /* handle stereo sound */
-                          /* FIXME: In theory a badguy object doesn't know anything about player objects */
-                          if (tux.base.x  > pbad->base.x)
-                            play_sound(sounds[SND_RICOCHET], SOUND_LEFT_SPEAKER);
-                          else if (tux.base.x  < pbad->base.x)
-                            play_sound(sounds[SND_RICOCHET], SOUND_RIGHT_SPEAKER);
-                          else
-                            play_sound(sounds[SND_RICOCHET], SOUND_CENTER_SPEAKER);
-                        }
+                      pbad->dir = RIGHT;
+                    }
+                  else if (issolid( pbad->base.x + pbad->base.width-1, (int) pbad->base.y))
+                    {
+                      pbad->dir = LEFT;
+                    }
+                  if(pbad->mode == KICK && changed != pbad->dir)
+                    {
+                      /* handle stereo sound */
+                      /* FIXME: In theory a badguy object doesn't know anything about player objects */
+                      if (tux.base.x  > pbad->base.x)
+                        play_sound(sounds[SND_RICOCHET], SOUND_LEFT_SPEAKER);
+                      else if (tux.base.x  < pbad->base.x)
+                        play_sound(sounds[SND_RICOCHET], SOUND_RIGHT_SPEAKER);
+                      else
+                        play_sound(sounds[SND_RICOCHET], SOUND_CENTER_SPEAKER);
                     }
-                }
 
+                }
 
               /* Fall if we get off the ground: */
 
@@ -229,22 +235,28 @@ void badguy_action(bad_guy_type* pbad)
 
               pbad->base.y = pbad->base.y + pbad->base.ym * frame_ratio;
 
-             if(physic_get_state(&pbad->physic) == -1)
-             physic_set_state(&pbad->physic,PH_VTU);
-             
-               if(issolid(pbad->base.x, pbad->base.y + 32))
-               {
-               physic_set_state(&pbad->physic,PH_VTU);
-               pbad->base.ym = -0.6;
-               }
-               else if(issolid(pbad->base.x, pbad->base.y - 1))
-               { /* This works, but isn't the best solution imagineable */
-               pbad->base.ym = physic_get_velocity(&pbad->physic,-6.);
-               }
-               else
-               {
-               pbad->base.ym = physic_get_velocity(&pbad->physic,6.);
-               }
+              if(physic_get_state(&pbad->physic) == -1)
+                {
+                  physic_set_state(&pbad->physic,PH_VT);
+                  physic_set_start_vy(&pbad->physic,0.);
+                }
+
+              if(issolid(pbad->base.x, pbad->base.y + 32))
+                {
+                  physic_set_state(&pbad->physic,PH_VT);
+                  physic_set_start_vy(&pbad->physic,6.);
+                  pbad->base.ym = physic_get_velocity(&pbad->physic);
+                }
+              else if(issolid(pbad->base.x, pbad->base.y - 1))
+                { /* This works, but isn't the best solution imagineable */
+                 physic_set_state(&pbad->physic,PH_VT);
+                  physic_set_start_vy(&pbad->physic,0.);
+                  pbad->base.ym = physic_get_velocity(&pbad->physic);
+                }
+              else
+                {
+                  pbad->base.ym = physic_get_velocity(&pbad->physic);
+                }
 
               if (pbad->base.y > screen->h)
                 pbad->base.alive = NO;
index 7f3c256..f6440a4 100644 (file)
@@ -30,19 +30,25 @@ pphysic->state = nstate;
 pphysic->start_time = st_get_ticks();
 }
 
-float physic_get_velocity(physic_type* pphysic, float start_velocity)
+void physic_set_start_vy(physic_type* pphysic, float start_vy)
 {
-return - (start_velocity - 10.* ((float)(st_get_ticks() - pphysic->start_time))/1000.);
+pphysic->start_vy = start_vy;
 }
 
-float physic_get_max_distance(physic_type* pphysic, float start_velocity)
+float physic_get_velocity(physic_type* pphysic)
 {
-return (start_velocity * start_velocity / 2.*10.);
+if(pphysic->state == PH_VT)
+return - (pphysic->start_vy - 10.* ((float)(st_get_ticks() - pphysic->start_time))/1000.);
 }
 
-unsigned int physic_get_max_time(physic_type* pphysic, float start_velocity)
+float physic_get_max_distance(physic_type* pphysic)
 {
-return (unsigned int)((start_velocity / 10.) * 1000);
+return (pphysic->start_vy * pphysic->start_vy / 2.*10.);
+}
+
+unsigned int physic_get_max_time(physic_type* pphysic)
+{
+return (unsigned int)((pphysic->start_vy / 10.) * 1000);
 }
 
 unsigned int physic_get_time_gone(physic_type* pphysic)
index 3730285..9150345 100644 (file)
@@ -16,7 +16,7 @@
 #include "timer.h"
 
 enum {
-  PH_VTU /* Vertical throw up. */
+  PH_VT, /* Vertical throw.*/
 };
 
 /* Physic type: */
@@ -24,6 +24,7 @@ enum {
 typedef struct physic_type
   {
       int state;
+      float start_vy;
       unsigned int start_time;
   }
 physic_type;
@@ -31,9 +32,10 @@ physic_type;
 void physic_init(physic_type* pphysic);
 int physic_get_state(physic_type* pphysic);
 void physic_set_state(physic_type* pphysic, int nstate);
-float physic_get_velocity(physic_type* pphysic, float start_velocity);
-float physic_get_max_distance(physic_type* pphysic, float start_velocity);
-unsigned int physic_get_max_time(physic_type* pphysic, float start_velocity);
+void physic_set_start_vy(physic_type* pphysic, float start_vy);
+float physic_get_velocity(physic_type* pphysic);
+float physic_get_max_distance(physic_type* pphysic);
+unsigned int physic_get_max_time(physic_type* pphysic);
 unsigned int physic_get_time_gone(physic_type* pphysic);
 
 #endif /*SUPERTUX_PHYSIC_H*/