360 Race Car Movement

Discussion in 'JS Plugins In Development' started by mogwai, Jan 19, 2017.

  1. mogwai

    mogwai 1984 Veteran

    Messages:
    876
    Likes Received:
    566
    Location:
    Bassett Nebraska
    First Language:
    English
    Primarily Uses:
    RMMV
    EDIT: the switch to the new forum took out my sample video




    This is my first RPG Maker MV javascript. I'm not making it for me to use, but just to see if I can... It's not a practical RPG movement and I don't see any market for a 360 frame charset, but like it's good to learn the MV javascript libraries and how they piece together or whatever. I've been working on it all day. It's not much because I got super frustrated when the sun went down and I wasted a rare nice winter day working on the collision/bounce which only works 30% of the time or else great overlaps.

    My proudest accomplishment was simply making the 24*15 frame thing work. spaceship!RC360.png


    This homemade 360 frame charset is from an old game I made in Flash. I'm not sure if the forum resized it, but it's just an example: needs to be named filename!RC360.png the !RC360 at the end.



    The geometry is from this tutorial. http://www.helixsoft.nl/articles/circle/sincos.htm I'm not sure, but I think it's the same tutorial I used in 2007 when I made the asteroids game in Flash.


    Version 0.1 - Not quite plug and play. The only reliable setting is the RCMovement.raceCarMode = true; to turn it on by event.

    Code:
    
    /*:
     * @plugindesc Game includes a 360º race car movement, mode.
     * @author Mogwai "Jake Jilg"
     *
     * @param On By Default
     * @desc Movement activated at start up... Else call by script: RCMovement.raceCarMode = true;
     * Default: false
     * @default false
     *
     * @param Car Speed
     * @desc The movement speed of the race car object.
     * Default: 7
     * @default 7
     *
     * @param Rotate Speed
     * @desc The rotation speed of the race car object.
     * Default: 3
     * @default 3
     *
     * @param Acceleration
     * @desc The deceleration speed of the race car object.
     * Default: 0.9
     * @default 0.9
     *
     * @param Deceleration
     * @desc The deceleration speed of the race car object.
     * Default: 0.9
     * @default 0.9
     */
    
    if(typeof RCMovement === "undefined")
        RCMovement = {};
    RCMovement.Parameters = PluginManager.parameters('360RCMovement');
    RCMovement.raceCarMode = JSON.parse(RCMovement.Parameters["On By Default"]);
    RCMovement.carSpeed = JSON.parse(RCMovement.Parameters["Car Speed"]);
    RCMovement.rotateSpeed = JSON.parse(RCMovement.Parameters["Rotate Speed"]);
    RCMovement.acceleration = JSON.parse(RCMovement.Parameters["Acceleration"]);
    RCMovement.deceleration = JSON.parse(RCMovement.Parameters["Deceleration"]);
    RCMovement.radian = 0.0174532925199; // Math.PI / 180;
    RCMovement.keysDown = [];
    ImageManager.isRaceCar = function(filename) {
        return filename.match(/\!RC360$/i) !== null;
    };
    Sprite_Character.prototype.setCharacterBitmap = function() {
        this.bitmap = ImageManager.loadCharacter(this._characterName);
        this._isRaceCar = ImageManager.isRaceCar(this._characterName);
        this._isBigCharacter = ImageManager.isBigCharacter(this._characterName);
    };
    Sprite_Character.prototype.characterBlockX = function() {
        var pw = this.patternWidth() / 48;
        if(this._character._x + pw < 0) //loop top and bottom
            this._character.setPosition(pw + $gameMap.width(), this._character._y);
        if(this._character._x > pw + $gameMap.width())
            this._character.setPosition(0 - pw, this._character._y);
        if (this._isBigCharacter || this._isRaceCar) {
            return 0;
        } else {
            var index = this._character.characterIndex();
            return index % 4 * 3;
        }
    };
    Sprite_Character.prototype.characterBlockY = function() {
        var ph = this.patternHeight() / 48;
        if(this._character._y + ph < 0) //loop top and bottom
            this._character.setPosition(this._character._x, ph + $gameMap.height());
        if(this._character._y > ph + $gameMap.height())
            this._character.setPosition(this._character._x, 0 - ph);
        if (this._isBigCharacter || this._isRaceCar) {
            return 0;
        } else {
            var index = this._character.characterIndex();
            return Math.floor(index / 4) * 4;
        }
    };
    Sprite_Character.prototype.patternWidth = function() {
        if (this._tileId > 0) {
            return $gameMap.tileWidth();
        } else if (this._isBigCharacter) {
            return this.bitmap.width / 3;
        } else if (this._isRaceCar) {
            return this.bitmap.width / 24;
        } else {
            return this.bitmap.width / 12;
        }
    };
    Sprite_Character.prototype.patternHeight = function() {
        if (this._tileId > 0) {
            return $gameMap.tileHeight();
        } else if (this._isBigCharacter) {
            return this.bitmap.height / 4;
        } else if (this._isRaceCar) {
            return this.bitmap.height / 15;
        } else {
            return this.bitmap.height / 8;
        }
    };
    Sprite_Character.prototype.characterPatternX = function() {
        var one_two_three =  (this._character.direction() - 2) / 2;
        return this._isRaceCar ?
            RCMovement.raceCarMode ?
                Math.floor(this._character.RCdir() / 15)
            :
                [0,18,6,12][one_two_three]
        :
            this._character.pattern();
    };
    Sprite_Character.prototype.characterPatternY = function() {
        return this._isRaceCar ?
            RCMovement.raceCarMode ? Math.round(this._character.RCdir()) % 15 : 0
        :
            (this._character.direction() - 2) / 2;;
    };
    Game_Player.prototype.RCdir = function(){
        if(typeof this._RCx_speed === "undefined")
            this._RCx_speed = 0
        if(typeof this._RCy_speed === "undefined")
            this._RCy_speed = 0
        if(typeof this._RCdir === "undefined")
            this._RCdir = {2:0, 4:270, 6:90, 8:180}[this._direction]; // 2down 4left 6right 8up
        if(typeof this._bouncing === "undefined")
            this._bouncing = false;
        var reverseDir = this._RCdir - 180;
        if(reverseDir < 0)
            reverseDir += 360;
        if(reverseDir > 360)
            reverseDir -= 360;
        return !this._bouncing ? this._RCdir : reverseDir;
    }
    document.addEventListener('keydown', function(e){
        var keyCode = e.keyCode || e.which;
        if(RCMovement.keysDown.indexOf(keyCode) === -1)
            RCMovement.keysDown.push(keyCode)
    });
    document.addEventListener('keyup', function(e){
        var keyCode = e.keyCode || e.which;
        if(RCMovement.keysDown.indexOf(keyCode) !== -1)
            RCMovement.keysDown.splice(RCMovement.keysDown.indexOf(keyCode), 1);
    });
    Game_Player.prototype.cachedMoveByInput = Game_Player.prototype.moveByInput;
    Game_Player.prototype.moveByInput = function() {
        return RCMovement.raceCarMode ? false : Game_Player.prototype.cachedMoveByInput.apply(this, arguments);
    };
    RCMovement.maxtest = function(n) {
        return n < RCMovement.carSpeed && n > 0 - RCMovement.carSpeed;
    }
    Game_CharacterBase.prototype.isMapPassable = function(x, y, d) {
        var x2 = $gameMap.roundXWithDirection(x, d);
        var y2 = $gameMap.roundYWithDirection(y, d);
        var d2 = this.reverseDir(d);
        var uptop = false;
        if(RCMovement.raceCarMode) {
            var d3 = {0:2, 270:4, 90:6, 180:8}[Math.floor(this._RCdir / 90) * 90];
            var x3 = $gameMap.roundXWithDirection(x, d3);
            var y3 = $gameMap.roundYWithDirection(y, d3);
            var uptop = y3 < 0 || y3 > $gameMap.height() || x3 < 0 || x3 > $gameMap.width();
        }
        return uptop ? true : $gameMap.isPassable(x, y, d) && $gameMap.isPassable(x2, y2, d2);
    };
    Game_Player.prototype.updateNonmoving = function() { // is Moving
        if(RCMovement.raceCarMode){
            var left = RCMovement.keysDown.indexOf(37) !== -1;
            var right = RCMovement.keysDown.indexOf(39) !== -1;
            if(left || right) {
                if(left) // turn left
                    this._RCdir -= RCMovement.rotateSpeed;
                else if(right) // turn right
                    this._RCdir += RCMovement.rotateSpeed;
            }
            if(this._RCdir < 0)
                this._RCdir += 360;
            if(this._RCdir > 360)
                this._RCdir -= 360;
            var xVelocity = Math.sin((this._RCdir * RCMovement.radian));
            var yVelocity = Math.cos((this._RCdir * RCMovement.radian));
            if(RCMovement.keysDown.indexOf(38) !== -1 && !this._bouncing) { //forward
                if(RCMovement.maxtest(this._RCx_speed + xVelocity))
                    this._RCx_speed += xVelocity;
                if(RCMovement.maxtest(this._RCy_speed + yVelocity))
                    this._RCy_speed += yVelocity;
            } else if (RCMovement.keysDown.indexOf(40) !== -1 && !this._bouncing) { //reverse
                if(RCMovement.maxtest(this._RCx_speed - xVelocity))
                    this._RCx_speed -= xVelocity;
                if(RCMovement.maxtest(this._RCy_speed - yVelocity))
                    this._RCy_speed -= yVelocity;
            }
            if(this._RCx_speed === 0 && this._RCy_speed === 0 && this._bouncing){
                this._bouncing = false;
                this._RCdir -= 180;
                if(this._RCdir < 0)
                    this._RCdir += 360;
                if(this._RCdir > 360)
                this._RCdir -= 360;  
            }
            if (this._RCx_speed !== 0 || this._RCy_speed !== 0){
                this._RCx_speed *= (Math.abs(this._RCx_speed) < 0.3 ? 0 : RCMovement.deceleration);
                this._RCy_speed *= (Math.abs(this._RCy_speed) < 0.3 ? 0 : RCMovement.deceleration);
            }
            var x = Math.round((this._RCx_speed * (1/48) * RCMovement.acceleration) * 100) / 100;
            var y = Math.round((this._RCy_speed * (1/48) * RCMovement.acceleration) * 100) / 100;
        }
        var d = {0:2, 270:4, 90:6, 180:8}[Math.floor(this._RCdir / 90) * 90];
        if(typeof $gameMap === "undefined") return;
        if(!$gameMap.isPassable(Math.round(this._x + x), Math.round(this._y + y), d) && !this._bouncing) {
            x = 0 - x * 2; // bounce
            y = 0 - y * 2;
            this._RCx_speed = 0 - this._RCx_speed;
            this._RCy_speed = 0 - this._RCy_speed;
            this._RCdir -= 180;
            if(this._RCdir < 0)
                this._RCdir += 360;
            if(this._RCdir > 360)
                this._RCdir -= 360;
            this._bouncing = true;
        }
        this._x += x; // pixel
        this._y += y; // velocity
        this._realX = this._x; // no transition
        this._realY = this._y;
    };
     
    Last edited: Feb 14, 2017
    #1
    ∊η∊ηra and Diretooth like this.

Share This Page