Posted by Bink on Sun 13 Jul 19:52
report abuse | download | new post
- // ***********************************************************
- //
- // Player Script
- //
- // This script runs the player's object.
- //
- // ***********************************************************
- //
- // This code setups a object that will contain information about a single weapon
- // that the player owns. An array of these objects will be created, one for each
- // weapon. This array will be used to install the weapons (in the construct) and
- // run the HUD.
- //
- // All this information could just be hard-code within the script, but this
- // way is more "object-oriented", easy to change and easy to read.
- //
- // Note that there is one additional weapon that is hard coded into the script. This
- // is the throwable grenade. This weapon is special as it is not selectable, but
- // instead auto-fired by a player message.
- //
- function playerWeapon(name,displayName,bitmapName,meshName,hasClip,hasAmmo)
- {
- this.name=name; // name of weapon
- this.displayName=displayName; // display name of weapon
- this.bitmapName=bitmapName; // name of bitmap in hud that represents this weapon
- this.meshName=meshName; // name of mesh in player model that represents this weapon
- this.hasClip=hasClip; // if this weapon uses clips
- this.hasAmmo=hasAmmo; // if this weapon has ammo
- }
- //
- // Now we create an array of these definitions. These arrays will be used to construct
- // the weapons in the rest of the script.
- //
- var weapons=new Array(7);
- weapons[0]=new playerWeapon('Hammer','Hammer','Hammer Icon','Hammer',false,false);
- weapons[1]=new playerWeapon('Pistol','Pistol','Pistol Icon','Pistol',true,true);
- weapons[2]=new playerWeapon('Machine Gun','AR14','Machine Gun Icon','Machine Gun',true,true);
- weapons[3]=new playerWeapon('Ray Gun','Ray Gun','Ray Gun Icon','Ray Gun',false,true);
- weapons[4]=new playerWeapon('Missile Launcher','Missile Launcher','Missile Icon','Missile Launcher',false,true);
- weapons[5]=new playerWeapon('Mine','Mines','Mine Icon','Mine',false,true);
- weapons[6]=new playerWeapon('Shock Launcher','Shock Gun','Shock Cube Icon','Shock Launcher',false,true);
- //
- // variables
- //
- var stopableAnimation=false;
- var canRespawn=false;
- var dying=false;
- var dieFlipCount=0;
- var lastHit=0;
- //
- // timer IDs
- //
- const PLAYER_DIE_FALL_OVER_ID=1;
- //
- // player script constructor
- //
- function playerConstruct(obj)
- {
- var i;
- // setup player model
- obj.model.on=true;
- obj.model.name='Player';
- obj.model.lit=DIM3_MODEL_LIT_VERTEX;
- obj.model.shadow.on=true;
- // setup player settings
- obj.setting.damage=true;
- obj.setting.crushable=true;
- obj.setting.bumpUp=true;
- obj.size.x=1730;
- obj.size.z=1730;
- obj.size.y=2200;
- obj.size.eyeOffset=-2100;
- obj.size.weight=250;
- obj.turnSpeed.facingWalk=4;
- obj.turnSpeed.motionWalk=4;
- obj.turnSpeed.facingRun=4.2;
- obj.turnSpeed.motionRun=4.2;
- obj.turnSpeed.facingCrawl=3;
- obj.turnSpeed.motionCrawl=3;
- obj.turnSpeed.facingAir=3;
- obj.turnSpeed.motionAir=3;
- obj.forwardSpeed.walk=95;
- obj.forwardSpeed.run=155;
- obj.forwardSpeed.crawl=50;
- obj.forwardSpeed.air=80;
- obj.forwardSpeed.acceleration=4;
- obj.forwardSpeed.deceleration=8;
- obj.forwardSpeed.accelerationAir=1.0;
- obj.forwardSpeed.decelerationAir=0.5;
- obj.sideSpeed.walk=60;
- obj.sideSpeed.run=120;
- obj.sideSpeed.crawl=30;
- obj.sideSpeed.air=50;
- obj.sideSpeed.acceleration=4;
- obj.sideSpeed.deceleration=8;
- obj.sideSpeed.accelerationAir=1.0;
- obj.sideSpeed.decelerationAir=0.5;
- obj.verticalSpeed.normal=50;
- obj.verticalSpeed.acceleration=10;
- obj.verticalSpeed.deceleration=20;
- obj.objectSpeed.jumpHeight=48;
- obj.objectSpeed.bumpHeight=300;
- obj.objectSpeed.duckAdd=32;
- obj.objectSpeed.duckChange=800;
- obj.look.upAngle=80;
- obj.look.downAngle=90;
- if (map.setting.multiplayer) { // different health for network games
- obj.health.maximum=100;
- obj.health.start=100;
- }
- else {
- obj.health.maximum=200;
- obj.health.start=200;
- }
- obj.health.fallDamageMinimumHeight=4000;
- obj.health.fallDamageFactor=0.01;
- obj.click.crosshairUp='click_up';
- obj.click.crosshairDown='click_down';
- // setup weapons from the array
- for (i=0;i!=weapons.length;i++) obj.weapon.add(weapons[i].name);
- // add the special grenade throw weapon
- // hide it so it's not selectable
- obj.weapon.add('Grenade Throw');
- obj.weapon.hideSingle('Grenade Throw');
- obj.radar.on=true;
- obj.radar.icon='player';
- }
- //
- // player spawing
- //
- function playerSpawn(obj)
- {
- // select the hammer
- obj.weapon.setSelect(weapons[0].name);
- // if in network play, hide all weapons
- // pickup weapons
- if (map.setting.multiplayer) {
- obj.weapon.hideSingle('Ray Gun',true);
- obj.weapon.hideSingle('Missile Launcher',true);
- obj.weapon.hideSingle('Mine',true);
- obj.weapon.hideSingle('Shock Launcher',true);
- }
- // redraw the weapon display
- redrawWeaponDisplay(obj);
- stopableAnimation=false;
- // setup health
- lastHit=0;
- // show hammer with model
- obj.model.mesh.showOnlyMesh('Default');
- obj.model.mesh.showMesh(weapons[0].meshName);
- // not dead
- canRespawn=false;
- dying=false;
- obj.event.startTimer(1,0);
- }
- //
- // player in liquids
- //
- function playerLiquid(obj)
- {
- sound.play("Splash",obj.position.x,obj.position.z,obj.position.y,1);
- }
- //
- // Player ANIMATION changes
- //
- function playerAnimation(obj,subEvent)
- {
- var model;
- // can't change animation if dying
- if (dying) return;
- // pick new animations
- model=obj.model;
- switch (subEvent) {
- case DIM3_EVENT_ANIMATION_OBJECT_STOP:
- if (stopableAnimation) model.animation.stop();
- if (obj.status.stand!=DIM3_STAND_DUCKING)
- model.animation.change('Idle')
- else
- model.animation.change('Idle Crawl');
- stopableAnimation=true;
- return;
- case DIM3_EVENT_ANIMATION_OBJECT_WALK:
- if (stopableAnimation) model.animation.stop();
- model.animation.change('Walk');
- stopableAnimation=true;
- return;
- case DIM3_EVENT_ANIMATION_OBJECT_RUN:
- if (stopableAnimation) model.animation.stop();
- model.animation.change('Run');
- stopableAnimation=true;
- return;
- case DIM3_EVENT_ANIMATION_OBJECT_WALK_BACK:
- if (stopableAnimation) model.animation.stop();
- model.animation.change('WalkBack');
- stopableAnimation=true;
- return;
- case DIM3_EVENT_ANIMATION_OBJECT_RUN_BACK:
- if (stopableAnimation) model.animation.stop();
- model.animation.change('RunBack');
- stopableAnimation=true;
- return;
- case DIM3_EVENT_ANIMATION_OBJECT_WALK_LEFT:
- case DIM3_EVENT_ANIMATION_OBJECT_WALK_LEFT_FORWARD:
- case DIM3_EVENT_ANIMATION_OBJECT_WALK_LEFT_BACK:
- case DIM3_EVENT_ANIMATION_OBJECT_RUN_LEFT:
- case DIM3_EVENT_ANIMATION_OBJECT_RUN_LEFT_FORWARD:
- case DIM3_EVENT_ANIMATION_OBJECT_RUN_LEFT_BACK:
- if (stopableAnimation) model.animation.stop();
- model.animation.change('WalkSideLeft');
- stopableAnimation=true;
- return;
- case DIM3_EVENT_ANIMATION_OBJECT_WALK_RIGHT:
- case DIM3_EVENT_ANIMATION_OBJECT_WALK_RIGHT_FORWARD:
- case DIM3_EVENT_ANIMATION_OBJECT_WALK_RIGHT_BACK:
- case DIM3_EVENT_ANIMATION_OBJECT_RUN_RIGHT:
- case DIM3_EVENT_ANIMATION_OBJECT_RUN_RIGHT_FORWARD:
- case DIM3_EVENT_ANIMATION_OBJECT_RUN_RIGHT_BACK:
- if (stopableAnimation) model.animation.stop();
- model.animation.change('WalkSideRight');
- stopableAnimation=true;
- return;
- case DIM3_EVENT_ANIMATION_OBJECT_CRAWL:
- case DIM3_EVENT_ANIMATION_OBJECT_CRAWL_BACK:
- case DIM3_EVENT_ANIMATION_OBJECT_CRAWL_LEFT:
- case DIM3_EVENT_ANIMATION_OBJECT_CRAWL_LEFT_FORWARD:
- case DIM3_EVENT_ANIMATION_OBJECT_CRAWL_LEFT_BACK:
- case DIM3_EVENT_ANIMATION_OBJECT_CRAWL_RIGHT:
- case DIM3_EVENT_ANIMATION_OBJECT_CRAWL_RIGHT_FORWARD:
- case DIM3_EVENT_ANIMATION_OBJECT_CRAWL_RIGHT_BACK:
- if (stopableAnimation) model.animation.stop();
- model.animation.change('Crawl');
- stopableAnimation=true;
- return;
- case DIM3_EVENT_ANIMATION_OBJECT_JUMP:
- model.animation.start('Jump');
- stopableAnimation=false;
- return;
- case DIM3_EVENT_ANIMATION_OBJECT_LAND:
- obj.motionVector.alterSpeed(0.7);
- model.animation.start('Land');
- stopableAnimation=false;
- return;
- case DIM3_EVENT_ANIMATION_OBJECT_DUCK_DOWN:
- model.animation.start('Squat');
- stopableAnimation=false;
- return;
- case DIM3_EVENT_ANIMATION_OBJECT_STAND_UP:
- model.animation.start('Standup');
- stopableAnimation=false;
- return;
- }
- }
- //
- // player health
- //
- function playerDamage(obj,tick)
- {
- // tint the view and update health bar
- obj.status.tintView(1,0,0,0.5,500,300,1000);
- // scream
- if (lastHit<tick) {
- lastHit=tick+600; // don't overplay scream effect
- sound.play("Scream",obj.position.x,obj.position.z,obj.position.y,utility.random.getFloat(0.75,1.0));
- }
- }
- function playerHealthDraw(obj)
- {
- }
- //
- // player item pickup
- //
- function playerItemPickup(obj)
- {
- obj.status.tintView(0.5,0.5,0.5,0.5,300,300,300); // flash gray on item pickup
- redrawWeaponDisplay(obj);
- }
- //
- // player dying
- //
- function playerDie(obj)
- {
- // hide weapon and health bar
- obj.weapon.hide(true);
- // freeze all input and lock off player
- obj.setting.damage=false;
- obj.model.shadow.on=false;
- obj.motionVector.stop();
- obj.motionAngle.turnStop();
- obj.status.freezeInput(true);
- // death scream
- sound.play("Scream",obj.position.x,obj.position.z,obj.position.y,utility.random.getFloat(0.4,0.5));
- // drop to floor
- dying=true;
- dieFlipCount=0;
- obj.model.animation.start('Die');
- obj.event.startTimer(1,PLAYER_DIE_FALL_OVER_ID);
- }
- function playerFallOver(obj)
- {
- // turn and drop
- dieFlipCount+=(1+(dieFlipCount*0.25)); // accelerated fall over
- // only slant camera if in fpp
- if (camera.setting.type==DIM3_CAMERA_TYPE_FPP) {
- camera.angle.z=dieFlipCount;
- obj.size.eyeOffset+=90;
- }
- // time to stop?
- if (dieFlipCount<=90) return;
- camera.angle.z=90;
- // clear timer and mark OK to respawn
- canRespawn=true;
- obj.setting.contact=false;
- obj.event.clearTimer();
- }
- //
- // player telefragging
- //
- function playerTelefrag(obj)
- {
- var x,y,z,yadd;
- obj.setting.hidden=true;
- obj.setting.contact=false;
- yadd=obj.size.y/4;
- x=obj.position.x;
- z=obj.position.z;
- y=obj.position.y-yadd;
- spawn.particle(x,z,y,'Telefrag Blood');
- y-=yadd;
- spawn.particle(x,z,y,'Telefrag Blood');
- y-=yadd;
- spawn.particle(x,z,y,'Telefrag Blood');
- sound.play('Splash',x,z,y,0.4);
- }
- //
- // change ammo read-outs
- //
- function make2DigitString(k)
- {
- if (k<10) return('0'+k);
- return(k);
- }
- function make3DigitString(k)
- {
- if (k<10) return('00'+k);
- if (k<100) return('0'+k);
- return(k);
- }
- function getAmmoString(obj,name)
- {
- var k,str;
- str=make2DigitString(obj.weapon.getAmmoCount(name));
- str=str+'/';
- str=str+make2DigitString(obj.weapon.getMaxAmmoCount(name));
- return(str);
- }
- function redrawWeaponDisplay(obj)
- {
- }
- //
- // player fires
- //
- function playerFire(obj)
- {
- obj.model.animation.interrupt('Fire');
- }
- //
- // player commands
- //
- function playerRespawn(obj)
- {
- // fix changes when dead
- camera.angle.z=0;
- obj.size.eyeOffset=-2100;
- obj.status.freezeInput(false);
- obj.weapon.hide(false);
- obj.setting.damage=true;
- obj.model.shadow.on=true;
- obj.setting.hidden=false;
- obj.setting.contact=true;
- // reset health
- obj.health.reset();
- obj.weapon.reset();
- // call spawn to set some other items
- // spawn only gets called when player starts, so we have
- // to call it ourselves here
- playerSpawn(obj);
- // restart differently depending on
- // if we are in a network game or not
- //
- // if not networking, then restart the map
- // from the last save point. If no last
- // save, then map automatically restarts
- // from the beginning
- //
- // if networking, then move the player
- // to any spot valid in the game rules
- if (!map.setting.multiplayer)
- map.action.restartMapFromSave()
- else
- obj.position.placeNetworkSpot();
- }
- function playerMessage(obj,subEvent,id)
- {
- // we are only checking for player_X key
- // message here, we can ignore all others
- if (subEvent!=DIM3_EVENT_MESSAGE_FROM_KEY_DOWN) return;
- // check messages
- switch (id) {
- case 0: // grenade throw is player_0
- obj.weapon.fire('Grenade Throw',3);
- redrawWeaponDisplay(obj);
- return;
- case 1: // respawn is player_1
- if (canRespawn) playerRespawn(obj);
- return;
- case 2: // flying mode is player_2
- if (!obj.setting.fly) {
- obj.setting.fly=true;
- obj.setting.inputMode=DIM3_INPUT_MODE_FLY;
- obj.motionVector.alterGravity(0); // reset gravity because now in flying
- iface.console.write('Fly Mode On');
- }
- else {
- obj.setting.fly=false;
- obj.setting.inputMode=DIM3_INPUT_MODE_FPP;
- iface.console.write('Fly Mode Off');
- }
- return;
- case 3: // camera mode is player_3
- if (camera.setting.type!=DIM3_CAMERA_TYPE_FPP) {
- camera.setting.type=DIM3_CAMERA_TYPE_FPP;
- obj.look.upAngle=80;
- obj.look.downAngle=35;
- obj.look.effectWeapons=true;
- iface.console.write('Camera Mode FPP');
- }
- else {
- camera.setting.type=DIM3_CAMERA_TYPE_CHASE;
- camera.chase.distance=4000;
- obj.look.upAngle=10;
- obj.look.downAngle=30;
- obj.look.effectWeapons=false;
- iface.console.write('Camera Mode Chase');
- }
- return;
- }
- }
- //
- // player timers
- //
- function playerTimer(obj,id)
- {
- switch (id) {
- case PLAYER_DIE_FALL_OVER_ID:
- playerFallOver(obj);
- return;
- }
- }
- //
- // player state
- // use these messages to persist or recover global variables when saving/loading
- //
- function playerState(obj,subEvent)
- {
- switch (subEvent) {
- case DIM3_EVENT_STATE_SAVE:
- data.addScriptSpecific('stopableAnimation',stopableAnimation);
- data.addScriptSpecific('canRespawn',canRespawn);
- data.addScriptSpecific('dying',dying);
- return;
- case DIM3_EVENT_STATE_LOAD:
- stopableAnimation=data.getScriptSpecific('stopableAnimation');
- canRespawn=data.getScriptSpecific('canRespawn');
- dying=data.getScriptSpecific('dying');
- return;
- }
- }
- //
- // player messages
- //
- function event(obj,mainEvent,subEvent,id,tick)
- {
- switch (mainEvent) {
- case DIM3_EVENT_CONSTRUCT:
- playerConstruct(obj);
- return;
- case DIM3_EVENT_SPAWN:
- playerSpawn(obj);
- return;
- case DIM3_EVENT_LIQUID:
- playerLiquid(obj);
- return;
- case DIM3_EVENT_ANIMATION_OBJECT:
- playerAnimation(obj,subEvent);
- return;
- case DIM3_EVENT_DAMAGE:
- playerDamage(obj,tick);
- return;
- case DIM3_EVENT_HEALTH:
- playerHealthDraw(obj);
- return;
- case DIM3_EVENT_CRUSH:
- case DIM3_EVENT_DIE:
- playerDie(obj);
- return;
- case DIM3_EVENT_TELEFRAG:
- playerTelefrag(obj);
- return;
- case DIM3_EVENT_PICKUP:
- playerItemPickup(obj);
- return;
- case DIM3_EVENT_WEAPON_SELECT:
- redrawWeaponDisplay(obj);
- return;
- case DIM3_EVENT_WEAPON_FIRE:
- playerFire(obj);
- redrawWeaponDisplay(obj);
- return;
- case DIM3_EVENT_MESSAGE:
- playerMessage(obj,subEvent,id);
- return;
- case DIM3_EVENT_TIMER:
- playerTimer(obj,id);
- return;
- case DIM3_EVENT_STATE:
- playerState(obj,subEvent);
- return;
- }
- }
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.