It is currently Fri Dec 15, 2017 8:23 am

All times are UTC + 9:30 hours




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Prototype Screen Shaking Code
PostPosted: Sun Sep 02, 2007 8:34 am 
Offline
User avatar

Joined: Sun Apr 01, 2007 3:14 am
Posts: 2228
Location: DE
Here's some functions and variables that will let you make the screen vibrate like any number of offensive and/or inappropriate things I could name here but won't. All code goes in BattleScreen.java.

First, add the following variables:
Code:
    /** Amount to offset the screen in the X direction. */
    private int offX = 0;
    /** Amount to offset the screen in the Y direction. */
    private int offY = 0;
    /** Remaining number frames to shake the screen. */
    private int shakeTime = 0;
    /** Intensity with which to shake the screen. This should be double the maximum offset. */
    private int shakeIntensity = 0;
    /** Causes the screen to shake less violently as time goes on. */
    private boolean shakeFade = false;


Add these functions somewhere:
Code:
    /** Updates the current screen offset */
    public void updateOffset(){
      if(shakeTime != 0){
          offX = b.getRNG().nextInt(shakeIntensity) - (shakeIntensity/2);
          offY = b.getRNG().nextInt(shakeIntensity) - (shakeIntensity/2);
          
          if(shakeFade && shakeIntensity > 0){
             shakeIntensity -= 1;
          }
          
          if(shakeTime > 0){
             shakeTime -= 1;
          }
       }
      else{
         offX = 0;
         offY = 0;
      }
    }


    /** Call this to make the screen start or stop shaking.
     * @param sIntensity The intensity with which the screen should shake. The screen will be offset by random numbers between positive and negative (sIntensity/2).
     * @param sLength The number of frames for which the screen should shake. If set to a negative number, shaking will continue until manually cancelled. If set to 0, shaking will be stopped.
     * @param fadeOut If true, the maximum intensity of the shaking will slowly decrease until either shakeTime expires or shakeIntensity reaches 0. */
    public void shakeScreen(int sIntensity, int sLength, boolean fadeOut){
       shakeIntensity = sIntensity;
       shakeTime = sLength;
       shakeFade = fadeOut;
    }


Use shakeScreen(...) to start and stop screen shaking.

In the function public void paintComponent(Graphics g), change the line
Code:
g.drawImage(bimg, 0, 0, this);

into
Code:
g.drawImage(bimg, offX, offY, this);


Finally, in public void drawScreen(Graphics2D g), add the following line (This may not be the best place to put it, but as long as this gets called exactly one time per frame, every frame, it should be okay.
Code:
updateOffset();


Now, wherever you want to make the screen shake, just add a call to shakeScreen(...).
Code:
// Causes the screen to offset up to 6 pixels in any direction for 10 frames
shakeScreen(13, 10, false);



Also, I don't know if anyone's actively working on the text parser yet, but if you are, don't forget to add control characters for screen shaking.

_________________
urusan wrote:
Do what Fugue said


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC + 9:30 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Blue Moon by Trent © 2007
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group