Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "BlockCommon.as"
- #include "IslandsCommon.as"
- const f32 BULLET_SPEED = 12.0f;
- const f32 BULLET_SPREAD = 1.0f;
- const f32 BULLET_LIFETIME = 0.75f;
- const f32 MIN_FIRE_PAUSE = 3.2f;//min wait between shots
- const f32 MAX_FIRE_PAUSE = 8.0f;//max wait between shots
- //todo: should be done with 1 factor value
- const f32 FIRE_PAUSE_DECREASE_FACTOR = 0.85f;//how fast it recuperates
- const f32 FIRE_PAUSE_INCREASE_FACTOR = 1.09f;//how quick it slows down
- Random _shotspreadrandom(0x11598); //clientside
- void onInit( CBlob@ this )
- {
- this.getCurrentScript().tickFrequency = 10;
- this.Tag("turret");
- this.Tag("fixed_gun");
- this.addCommandID("fire");
- this.addCommandID("disable");
- this.set_string("barrel", "left");
- if ( getNet().isServer() )
- {
- this.set_f32("fire pause",MIN_FIRE_PAUSE);
- this.Sync("fire pause", true );
- }
- CSprite@ sprite = this.getSprite();
- CSpriteLayer@ layer = sprite.addSpriteLayer( "turret", 16, 16 );
- if (layer !is null)
- {
- layer.SetRelativeZ(2);
- layer.SetLighting( false );
- Animation@ anim = layer.addAnimation( "fire left", MIN_FIRE_PAUSE/2.0f, false );
- anim.AddFrame(Block::AUTOCANNON_F2);
- anim.AddFrame(Block::AUTOCANNON_F1);
- Animation@ anim2 = layer.addAnimation( "fire right", MIN_FIRE_PAUSE/2.0f, false );
- anim2.AddFrame(Block::AUTOCANNON_F3);
- anim2.AddFrame(Block::AUTOCANNON_F1);
- Animation@ anim3 = layer.addAnimation( "default", 1.0f, false );
- anim3.AddFrame(Block::AUTOCANNON_F1);
- layer.SetAnimation("default");
- }
- this.set_u32("fire time", 0);
- }
- void onTick( CBlob@ this )
- {
- if (this.getShape().getVars().customData <= 0)//not placed yet
- return;
- f32 currentFirePause = this.get_f32("fire pause");
- if (currentFirePause > MIN_FIRE_PAUSE)
- this.set_f32("fire pause", currentFirePause * FIRE_PAUSE_DECREASE_FACTOR);
- //print( "Fire pause: " + currentFirePause );
- }
- bool canShoot( CBlob@ this )
- {
- return ( this.get_u32("fire time") + this.get_f32("fire pause") < getGameTime() ) && getRules().get_u16( "projectiles" ) < 6;
- }
- void onCommand( CBlob@ this, u8 cmd, CBitStream @params )
- {
- if (cmd == this.getCommandID("fire"))
- {
- if (canShoot(this))
- {
- u8 teamNum = params.read_u8();
- CSprite@ sprite = this.getSprite();
- CSpriteLayer@ layer = sprite.getSpriteLayer( "turret" );
- layer.SetAnimation( "default" );
- if (this.get_string("barrel") == "left")
- layer.SetAnimation( "fire left" );
- if (this.get_string("barrel") == "right")
- layer.SetAnimation( "fire right" );
- Vec2f offset(_shotspreadrandom.NextFloat() * BULLET_SPREAD,0);
- offset.RotateBy(_shotspreadrandom.NextFloat() * 360.0f, Vec2f());
- Vec2f aimvector = Vec2f(1, 0).RotateBy(this.getAngleDegrees());
- Vec2f velocity = (aimvector * BULLET_SPEED) + offset;
- f32 time = BULLET_LIFETIME;
- Vec2f pos = this.getPosition() + aimvector*9;
- this.set_u32("fire time", getGameTime());
- Vec2f barrelOffset = Vec2f(0, 0);
- if (this.get_string("barrel") == "left")
- {
- barrelOffset = Vec2f(0, -3.0).RotateBy(-velocity.Angle());
- this.set_string("barrel", "right");
- }
- else
- {
- barrelOffset = Vec2f(0, 3.0).RotateBy(-velocity.Angle());
- this.set_string("barrel", "left");
- }
- pos = pos + barrelOffset;
- shotParticles(pos, velocity.Angle(), true );
- if (getNet().isServer() && 1 == 2)
- {
- CBlob@ bullet = server_CreateBlob( "bullet", teamNum, pos );
- if (bullet !is null)
- {
- bullet.setVelocity( velocity + ((getIsland(this) !is null) ? getIsland( this ).vel : Vec2f(0, 0)) );
- bullet.server_SetTimeToDie( time );
- }
- }
- if( 1 == 1 )
- {
- HitInfo@[] hitInfos;
- CMap@ map = this.getMap();
- if( map.getHitInfosFromRay( pos, -aimvector.Angle(), 180.0f, this, @hitInfos ) )
- {
- for (uint i = 0; i < hitInfos.length; i++)
- {
- HitInfo@ hi = hitInfos[i];
- CBlob@ b = hi.blob;
- if(b is null || b is this) continue;
- const int color = b.getShape().getVars().customData;
- const int blockType = b.getSprite().getFrame();
- const bool isBlock = b.getName() == "block";
- bool killed = false;
- if ( !b.hasTag( "booty" ) && (color > 0 || !isBlock) )
- {
- if (isBlock )
- {
- if ( ( ( Block::isCore(blockType) || b.hasTag("turret") || blockType == Block::BOMB || blockType == Block::SEAT ) && b.getTeamNum() != this.getTeamNum() ) || Block::isSolid(blockType) )//hit these and die
- killed = true;
- else
- continue;
- }
- else
- {
- if ( b.getTeamNum() == this.getTeamNum() || ( b.hasTag("player") && b.isAttached() ) )
- continue;
- }
- shotParticles( hi.hitpos, -aimvector.Angle(), false );
- this.server_Hit( b, pos,
- Vec2f_zero, 0.1f,
- 0, true);
- }
- }
- }
- }
- if (XORRandom(1) == 0)
- sprite.PlaySound("Gunshot2.ogg");
- else
- sprite.PlaySound("Gunshot3.ogg");
- f32 currentFirePause = this.get_f32("fire pause");
- if (currentFirePause < MAX_FIRE_PAUSE)
- this.set_f32("fire pause", Maths::Pow(currentFirePause, FIRE_PAUSE_INCREASE_FACTOR) );
- }
- }
- }
- Random _shotrandom(0x15125); //clientside
- void shotParticles(Vec2f pos, float angle, bool muzzle )
- {
- //muzzle flash
- {
- string particle = "Entities/Block/turret_muzzle_flash.png";
- if( !muzzle )
- {
- particle = "Entities/Block/turret_smoke.png";
- }
- CParticle@ p = ParticleAnimated( particle,
- pos, Vec2f(),
- -angle, //angle
- 1.0f, //scale
- 3, //animtime
- 0.0f, //gravity
- true ); //selflit
- if(p !is null)
- p.Z = 10.0f;
- }
- Vec2f shot_vel = Vec2f(0.5f,0);
- shot_vel.RotateBy(-angle);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement