猫史档案馆


教你做游戏第三章:怪物与动物

用户:2333280823332808查看:0 回复:0 评论:0 创建时间:2024-06-21T13:36:48


world.addCollisionFilter('.creature', '.remove collides')
world.onPlayerJoin(({entity:e})=>{
    e.take_hun=(number)=>{
        if(e.bsd>=number){
            e.bsd-=number
        }else{
            e.hungry-=number-e.bsd
            e.bsd=0
            if(e.hungry<0){
                e.hungry=0
            }
        }
    }
})
class basic_monster{
    constructor(self){
        this.corner=0
        this.self=self
        this.speed=self.speed
        this.orientation=new GameQuaternion(0,0,0,1)
        this.load=0
        this.jump_load=0
        this.lock=false
        this.walk_motion=self.walk_motion
        this.hurt_motion=self.hurt_motion
        this.attack_dist=self.attack_dist
        this.find_dist=self.find_dist
        this.maxload=self.maxload
        this.hurt=self.hurt_number
        this.walk_motion.onFinish(()=>{
            if(!this.lock&&!this.self.destroyed){
                this.walk_motion.play()
            }
        })
        this.hurt_motion.onFinish(()=>{
            this.lock=false
            if(this.self.destroyed)return
            this.walk_motion.play()
        })
    }
    find(target){
        if(target){
            this.dist=this.self.position.distance(target.position)
            if(this.dist<this.find_dist){
                this.target=target
                if(this.lock){
                    this.walk_motion.play()
                    this.lock=false
                }
            }else{
                this.target=false
                this.lock=true
            }
        }
    }
    goto(tick){
        if(this.self.voxelContacts[0]){
            this.self.velocity.x=this.direct.x*this.speed
            this.self.velocity.z=this.direct.z*this.speed
            if(tick%4==0){
                if(voxels.getVoxel(this.self.position.x+this.direct.x,this.self.position.y-0.3,this.self.position.z+this.direct.z)&&this.jump_load<=0){
                    this.self.velocity.y=0.4
                    this.jump_load=1
                }
            }
        }
    }
    run(tick){
        if(this.target){
            if(!(this.target.hp>0&&(this.target.isPlayer?!this.target.player.invisible:true))){
                this.target=false
                return
            }
            if(this.self.destroyed)return
            this.dist=this.self.position.distance(this.target.position)
            this.direct=vec_mul(this.target.position.sub(this.self.position),1/this.dist)
            this.self.meshOrientation.copy(this.orientation.rotateY(Math.atan2(this.direct.z,this.direct.x)+this.corner))
            if(tick%16==0){
                this.load-=1
                this.jump_load-=1
            }
            if(this.dist<=this.attack_dist){
                this.attack(tick)
            }else{
                this.goto(tick)
            }
        }else{
            this.lock=true
            this.self.velocity=new GameVector3(0,0,0)
        }
        this.other(tick)
    }
    other(tick){

    }
    attack(tick){
        if(this.load<=0){
            this.goto(tick)
            repel(this.target,new GameVector3(this.direct.x,0.6,this.direct.z))
            hurt(this.target,this.hurt,this.self,"战斗")
            this.lock=true
            this.hurt_motion.play()
            this.load=this.maxload
        }
    }
}
class zombie extends basic_monster{
    constructor(self){
        super(self)
        this.corner=Math.PI
    }
    other(tick){
        if(tick%16==0){
            give_buff(this.self,new buff("燃烧",1,10,this.self))
        }
    }
    
}
class 喵 extends basic_monster{
    constructor(self){
        super(self)
        this.corner=-Math.PI/2
        this.target=false
    }
    find(){

    }
    other(tick){
        if(tick%16==0&&this.target){
            if(Math.random()<0.5){
                this.stand_list=find_stand_place(this.target.position,7,5)
                this.self.position.copy(this.stand_list[randint(0,this.stand_list.length-1)])
            }
        }
    }
}
class skeleton extends basic_monster{
    constructor(self){
        super(self)
        this.corner=-Math.PI/2
    }
    attack(){
        this.lock=true
        this.hurt_motion.play()
        if(this.load<=0){
            this.arrow=world.createEntity({
                position:this.self.position.add(new GameVector3(0,0.405,0)),
                mesh:"mesh/箭.vb",
                friction:1,
                gravity:false,
                particleRate:100,
                particleColor:new GameRGBColor(1,1,1),
            })
            this.arrow.bounds=new GameVector3(0.01,0.01,0.01)
            this.arrow.fixed=true
            this.arrow.e=this.self
            this.arrow.addTag("physic")
            this.arrow.physic={f:0.05,g:0.1}
            this.vec=this.direct
            this.arrow.meshOrientation=(new GameQuaternion(0,0,0,1)).rotateY(Math.atan2(this.direct.z, this.direct.x)).rotateX(Math.sinh(this.direct.y))
            this.arrow.velocity=this.self.velocity.add(vec_mul(this.direct,5))
            this.enchanting={}
            contact_func(this.arrow,(e,o)=>{
            hurt(o,e.velocity.mag()*2*(1+not_null(this.enchanting["力量"]*0.25,0)),e.e,"弹射物")
                e.destroy()
                repel(o,new GameVector3(-(e.position.x-o.position.x),-(e.position.y-o.position.y),-(e.position.z-o.position.z)))
                },"game",[this.arrow,this.self],)
            this.arrow.onVoxelContact(({entity:e})=>{
                e.velocity=new GameVector3(0,0,0)
            })
            wait_destroy(this.arrow,5000)
            this.load=5
        }
    }
}
class creeper{
        constructor(self){
        this.corner=-Math.PI/2
        this.self=self
        this.speed=self.speed
        this.orientation=new GameQuaternion(0,0,0,1)
        this.load=0
        this.jump_load=0
        this.lock=false
        this.walk_motion=self.walk_motion
        this.hurt_motion=self.hurt_motion
        this.attack_dist=self.attack_dist
        this.find_dist=self.find_dist
        this.maxload=self.maxload
        this.hurt=self.hurt_number
        this.walk_motion_player=false
        this.walk_motion.onFinish(()=>{
            if(!this.lock&&!this.self.destroyed&&!this.walk_motion_player){
                this.walk_motion.play()
            }else{
                this.walk_motion_player=false
            }
        })
        this.hurt_motion_play=false
        this.hurt_motion.onFinish(()=>{
            if(this.dist>5){
                this.lock=false
                if(this.self.destroyed)return
                if(!this.walk_motion_player){
                    this.walk_motion.play()
                    this.walk_motion_player=true
                }
                this.hurt_motion_play=false
            }else{
                this.hurt_motion.play()
                this.walk_motion_player
            }
        })
    }
    run(tick){
        if(this.target){
            if(!(this.target.hp>0&&(this.target.isPlayer?!this.target.player.invisible:true))){
                this.target=false
                return
            }
            if(this.self.destroyed)return
            this.dist=this.self.position.distance(this.target.position)
            this.direct=vec_mul(this.target.position.sub(this.self.position),1/this.dist)
            this.self.meshOrientation.copy(this.orientation.rotateY(Math.atan2(this.direct.z,this.direct.x)+this.corner))
            if(tick%16==0){
                this.load-=1
                this.jump_load-=1
            }
            if(this.dist<=this.attack_dist){
                this.attack(tick)
            }else{
                this.goto(tick)
            }
        }else{
            this.lock=true
            this.self.velocity=new GameVector3(0,0,0)
        }
    }
    find(target){
        if(target){
            this.dist=this.self.position.distance(target.position)
            if(this.dist<this.find_dist){
                this.target=target
                if(this.lock&&!this.walk_motion_player){
                    this.walk_motion.play()
                    this.lock=false
                    this.walk_motion_player=true
                }
            }else{
                this.walk_motion_player=false
                this.target=false
                this.lock=true
            }
        }
    }
    goto(tick){
        if(this.dist>5){
            this.boom_time=0
        }
        if(this.self.voxelContacts[0]){
            this.self.velocity.x=this.direct.x*this.speed
            this.self.velocity.z=this.direct.z*this.speed
            if(tick%4==0){
                if(voxels.getVoxel(this.self.position.x+this.direct.x,this.self.position.y-0.3,this.self.position.z+this.direct.z)&&this.jump_load<=0){
                    this.self.velocity.y=0.4
                }
            }
        }
    }
    attack(tick){
        this.goto(tick)
        if(tick%16==0){
            if(!this.hurt_motion_play){
                this.hurt_motion.play()
                this.hurt_motion_play=true
                this.walk_motion_player=false
            }
            this.boom_time+=1
        }
        if(this.boom_time>=3){
            this.self.destroy()
            boom(this.self.position,3,50,1,this.self)
        }
    }
}
class spider extends basic_monster{
    constructor(self){
        super(self)
        this.corner=-Math.PI/2
        this.boom_time=0
    }
    goto(tick){
        if(this.self.voxelContacts[0]&&world.sunPhase>0.5){
            this.lock=false
            this.self.velocity.x=this.direct.x*this.speed
            this.self.velocity.z=this.direct.z*this.speed
            if(tick%4==0){
                if(voxels.getVoxel(this.self.position.x+this.direct.x,this.self.position.y-0.3,this.self.position.z+this.direct.z)){
                    this.self.velocity.y=0.4
                }
            }
        }else{
            this.lock=true
        }
    }
    attack(tick){
        if(this.load<=0){
            this.goto(tick)
            repel(this.target,new GameVector3(this.direct.x,0.6,this.direct.z))
            hurt(this.target,this.hurt,this.self,"战斗")
            this.lock=true
            this.hurt_motion.play()
            this.load=this.maxload
            if(this.self.name=="洞穴蜘蛛"){
                give_buff(this.target,new buff("中毒",1,10,this.target,this.self))
            }
        }
    }
}
function set_zombie(position){
    e=world.createEntity({
        mesh:"mesh/僵尸.vb",
        position:position,
        gravity:true,
        meshScale:new GameVector3(0.0625,0.0625,0.0625),
        collides:true,
        friction:1
    })
    e.addTag("remove colldies")
    e.addTag("monster")
    set_new_entity(e)
    e.maxHp=20
    e.exp=5
    e.name="僵尸"
    e.hp=e.maxHp
    e.type={type:"entity",enable_move:true,creature_type:"bug"}
    e.maxload=3
    e.speed=0.3
    e.find_dist=40
    e.attack_dist=1.5
    if(Math.random()<0.05){
        e.mesh="mesh/小僵尸.vb"
        e.speed=0.6
        e.name="小僵尸"
        e.exp=7
        e.maxload=2
        e.meshScale=vec_mul(e.meshScale,0.5)
        e.maxHp=10
        e.hp=e.maxHp
    }
    e.walk_motion=e.motion.loadByName("移动")
    e.hurt_motion=e.motion.loadByName("边移动边攻击")
    e.hurt_number=4
    e.pathfinding=new zombie(e)
    e.onDie(({attacker:a,entity:e})=>{
        rob=1
        if(a){
            if(a.isPlayer){
                give_exp(a,e.exp)
                if(a.right_hand.type.includes("tool")){
                    rob=a.right_hand.get_number("rob")
                }
            }
        }
        e.destroy()
        dropper(set_item({name:"腐肉",number:randint(0,2*rob)}),e.position)
        if(Math.random()<0.01*(2**rob))dropper(set_item({name:"胡萝卜",number:1}),e.position)
        if(Math.random()<0.01*(2**rob))dropper(set_item({name:"马铃薯",number:1}),e.position)
        if(Math.random()<0.01*(2**rob))dropper(set_item({name:"铁锭",number:1}),e.position)
    })
    monsters.push(e)
}
function set_spider(position){
    e=world.createEntity({
        mesh:"mesh/蜘蛛.vb",
        position:position,
        gravity:true,
        meshScale:new GameVector3(0.0625,0.0625,0.0625),
        collides:true,
        friction:1
    })
    e.addTag("remove colldies")
    e.addTag("monster")
    set_new_entity(e)
    e.maxHp=16
    e.exp=5
    e.name="蜘蛛"
    e.hp=e.maxHp
    e.type={type:"entity",enable_move:true,creature_type:"bug"}
    e.maxload=3
    e.speed=0.4
    e.find_dist=25
    e.attack_dist=1.5
    if(Math.random()<0.05){
        e.mesh="mesh/洞穴蜘蛛.vb"
        e.speed=0.3
        e.name="洞穴蜘蛛"
        e.exp=7
    }
    e.walk_motion=e.motion.loadByName("移动")
    e.hurt_motion=e.motion.loadByName("边移动边攻击")
    e.hurt_number=4
    e.pathfinding=new spider(e)
    e.onDie(({attacker:a,entity:e})=>{
        rob=1
        if(a){
            if(a.isPlayer){
                give_exp(a,e.exp)
                if(a.right_hand.type.includes("tool")){
                    rob=a.right_hand.get_number("rob")
                }
            }
        }
        e.destroy()
        if(Math.random()<0.5*rob)dropper(set_item({name:"线",number:1}),e.position)
        if(Math.random()<0.25*rob)dropper(set_item({name:"蜘蛛眼",number:1}),e.position)
    })
    monsters.push(e)
}
function set_skeleton(position){
    e=world.createEntity({
        mesh:"mesh/骷髅.vb",
        position:position,
        gravity:true,
        meshScale:new GameVector3(0.0625,0.0625,0.0625),
        collides:true,
        friction:1
    })
    e.addTag("remove colldies")
    e.addTag("monster")
    set_new_entity(e)
    e.maxHp=16
    e.exp=5
    e.name="骷髅"
    e.hp=e.maxHp
    e.type={type:"entity",enable_move:true,creature_type:"death"}
    e.maxload=4
    e.speed=0.2
    e.find_dist=35
    e.attack_dist=20
    e.walk_motion=e.motion.loadByName("没持弓移动")
    e.hurt_motion=e.motion.loadByName("持弓")
    e.pathfinding=new skeleton(e)
    e.onDie(({attacker:a,entity:e})=>{
        rob=1
        if(a){
            if(a.isPlayer){
                give_exp(a,e.exp)
                if(a.right_hand.type.includes("tool")){
                    rob=a.right_hand.get_number("rob")
                }
            }
        }
        e.destroy()
        dropper(set_item({name:"骨头",number:randint(0,1*rob)}),e.position)
    })
    monsters.push(e)
}
function set_creeper(position){
    e=world.createEntity({
        mesh:"mesh/苦力怕.vb",
        position:position,
        gravity:true,
        meshScale:new GameVector3(0.0625,0.0625,0.0625),
        collides:true,
        friction:1
    })
    e.addTag("remove colldies")
    e.addTag("monster")
    set_new_entity(e)
    e.maxHp=16
    e.exp=5
    e.name="爬行者"
    e.hp=e.maxHp
    e.type={type:"entity",enable_move:true,creature_type:"???"}
    e.maxload=0
    e.speed=0.3
    e.find_dist=32
    e.attack_dist=4
    e.walk_motion=e.motion.loadByName("移动")
    e.hurt_motion=e.motion.loadByName("边移动边攻击")
    e.pathfinding=new creeper(e)
    e.onDie(({attacker:a,entity:e})=>{
        rob=1
        if(a){
            if(a.isPlayer){
                give_exp(a,e.exp)
                if(a.right_hand.type.includes("tool")){
                    rob=a.right_hand.get_number("rob")
                }
            }
        }
        e.destroy()
        dropper(set_item({name:"火药",number:randint(0,1*rob)}),e.position)
    })
    monsters.push(e)
}
function set_喵(position){
    e=world.createEntity({
        mesh:"mesh/末影人.vb",
        position:position,
        gravity:true,
        meshScale:new GameVector3(0.0625,0.0625,0.0625),
        collides:true,
        friction:1
    })
    e.addTag("remove colldies")
    e.addTag("monster")
    set_new_entity(e)
    e.maxHp=30
    e.exp=15
    e.name="末影人"
    e.hp=e.maxHp
    e.type={type:"entity",enable_move:true,creature_type:"???"}
    e.maxload=3
    e.speed=0.5
    e.find_dist=30
    e.attack_dist=1.5
    e.walk_motion=e.motion.loadByName("移动")
    e.hurt_motion=e.motion.loadByName("边移动边攻击")
    e.hurt_number=9
    e.pathfinding=new 喵(e)
    e.onTakeDamage(({attacker:a})=>{
        if(a){
            e.pathfinding.target=a
        }
    })
    e.onDie(({attacker:a,entity:e})=>{
        rob=1
        if(a){
            if(a.isPlayer){
                give_exp(a,e.exp)
                if(a.right_hand.type.includes("tool")){
                    rob=a.right_hand.get_number("rob")
                }
            }
        }
        e.destroy()
        dropper(set_item({name:"末影之眼",number:1*rob}),e.position)
    })
    monsters.push(e)
}
let monsters=[]
let players=[]
let animals=[]
world.onTick(({tick})=>{
    if(tick%32===0){
        monsters=world.querySelectorAll(".monster")
        players=world.querySelectorAll("player")
        animals=world.querySelectorAll(".animal")
        monsters.forEach((e) => {
            zomPos = e.position
            targets = players.sort((a,b)=>{
                return a.position.distance(zomPos)-b.position.distance(zomPos)
            })
            for(i in targets){
                if(targets[i].hp>0&&(targets[i].isPlayer?!targets[i].player.invisible:true)){
                    e.pathfinding.find(targets[i])
                    return
                }
            }
            e.pathfinding.target=false
        })
    }
    monsters.forEach(e=>{
        e.pathfinding.run(tick)
    })
    animals.forEach(e=>{
        e.pathfinding.run(tick)
    })
})
class basic_animal{
    constructor(self){
        this.corner=-Math.PI/2
        this.self=self
        this.speed=self.speed
        this.orientation=new GameQuaternion(0,0,0,1)
        this.jump_load=0
        this.walk_motion=self.walk_motion
        this.walk_motion.onFinish(()=>{
            if(!this.self.destroyed){
                this.walk_motion.play()
            }
        })
        this.walk_motion.play()
        this.random_target()
    }
    random_target(){
        this.target_angle=(Math.random()-0.5)*Math.PI*2
        this.target=new GameVector3(Math.sin(this.target_angle),0,Math.cos(this.target_angle))
    }
    check_target(){
        this.check_position=this.self.position.add(vec_mul(this.target,10))
        if(this.check_position.x<0||this.check_position.x>voxels.shape.x||this.check_position.z<0||this.check_position.z>voxels.shape.z){
            return false
        }
        return true
    }
    goto(tick){
        if(this.self.voxelContacts[0]){
            this.self.velocity.x=this.direct.x*this.speed
            this.self.velocity.z=this.direct.z*this.speed
            if(tick%4==0){
                if(voxels.getVoxel(this.self.position.x+this.direct.x,this.self.position.y-0.3,this.self.position.z+this.direct.z)&&this.jump_load<=0){
                    this.self.velocity.y=0.4
                    this.jump_load=1
                }
            }
        }
    }
    run(tick){
        if(this.self.destroyed)return
        this.direct=this.target
        this.self.meshOrientation.copy(this.orientation.rotateY(Math.atan2(this.direct.z,this.direct.x)+this.corner))
        if(tick%16==0){
            this.jump_load-=1
            if(Math.random()<0.2){
                this.random_target()
            }
            while(!this.check_target()){
                this.random_target()
            }
        }
        this.goto(tick)
    }
}
function set_pig(position){
    e=world.createEntity({
        mesh:"mesh/猪.vb",
        position:position,
        gravity:true,
        meshScale:new GameVector3(0.0625,0.0625,0.0625),
        collides:true,
        friction:1
    })
    e.addTag("remove colldies")
    e.addTag("animal")
    set_new_entity(e)
    e.maxHp=10
    e.exp=5
    e.name="猪"
    e.hp=e.maxHp
    e.type={type:"entity",enable_move:true,creature_type:"animal"}
    e.speed=0.4
    e.walk_motion=e.motion.loadByName("移动")
    e.pathfinding=new basic_animal(e)
    e.onTakeDamage(({attacker:a})=>{
        e.other_position=new GameVector3(e.position.x-a.position.x,0,e.position.z-a.position.z)
        e.pathfinding.target=vec_mul(e.other_position,1/e.other_position.mag())
        give_buff(e,new buff("速度",2,10,e))
    })
    e.onDie(({attacker:a,entity:e,damageType:t})=>{
        rob=1
        if(a){
            if(a.isPlayer){
                give_exp(a,e.exp)
                if(a.right_hand.type.includes("tool")){
                    rob=a.right_hand.get_number("rob")
                }
            }
        }
        e.destroy()
        dropper(set_item({name:t=="燃烧"?"熟猪排":"生猪排",number:randint(1,1*rob)}),e.position)
    })
    animals.push(e)
}
function set_cow(position){
    e=world.createEntity({
        mesh:"mesh/牛.vb",
        position:position,
        gravity:true,
        meshScale:new GameVector3(0.0625,0.0625,0.0625),
        collides:true,
        friction:1
    })
    e.addTag("remove colldies")
    e.addTag("animal")
    set_new_entity(e)
    e.maxHp=16
    e.exp=5
    e.name="牛"
    e.hp=e.maxHp
    e.type={type:"entity",enable_move:true,creature_type:"animal"}
    e.speed=0.4
    e.walk_motion=e.motion.loadByName("移动")
    e.pathfinding=new basic_animal(e)
    e.onTakeDamage(({attacker:a})=>{
        e.other_position=new GameVector3(e.position.x-a.position.x,0,e.position.z-a.position.z)
        e.pathfinding.target=vec_mul(e.other_position,1/e.other_position.mag())
        give_buff(e,new buff("速度",2,10,e))
    })
    e.onDie(({attacker:a,entity:e,damageType:t})=>{
        rob=1
        if(a){
            if(a.isPlayer){
                give_exp(a,e.exp)
                if(a.right_hand.type.includes("tool")){
                    rob=a.right_hand.get_number("rob")
                }
            }
        }
        e.destroy()
        dropper(set_item({name:t=="燃烧"?"熟牛排":"生牛排",number:randint(1,1*rob)}),e.position)
        dropper(set_item({name:"皮革",number:randint(0,1*(1+(rob-1)*0.5))}),e.position)
    })
    animals.push(e)
}
class chicken extends basic_animal{
    run(tick){
        if(this.self.destroyed)return
        this.direct=this.target
        this.self.meshOrientation.copy(this.orientation.rotateY(Math.atan2(this.direct.z,this.direct.x)+this.corner))
        if(tick%16==0){
            this.jump_load-=1
            if(Math.random()<0.2){
                this.random_target()
            }console.log("a")
            if(Math.random()<0.005){
                dropper(set_item({name:"鸡蛋",number:1}),this.self.position)
            }
            while(!this.check_target()){
                this.random_target()
            }
        }
        this.goto(tick)
    }
}
function set_chicken(position){
    e=world.createEntity({
        mesh:"mesh/鸡.vb",
        position:position,
        gravity:true,
        meshScale:new GameVector3(0.0625,0.0625,0.0625),
        collides:true,
        friction:1
    })
    e.addTag("remove colldies")
    e.addTag("animal")
    set_new_entity(e)
    e.maxHp=5
    e.exp=3
    e.name="鸡"
    e.hp=e.maxHp
    e.type={type:"entity",enable_move:true,creature_type:"animal"}
    e.speed=0.4
    e.walk_motion=e.motion.loadByName("移动")
    e.pathfinding=new chicken(e)
    slowdown_buff=new buff("缓降",1,1,e)
    slowdown_buff.lock=true
    give_buff(e,slowdown_buff)
    e.onTakeDamage(({attacker:a})=>{
        e.other_position=new GameVector3(e.position.x-a.position.x,0,e.position.z-a.position.z)
        e.pathfinding.target=vec_mul(e.other_position,1/e.other_position.mag())
        give_buff(e,new buff("速度",2,10,e))
    })
    e.onDie(({attacker:a,entity:e,damageType:t})=>{
        rob=1
        if(a){
            if(a.isPlayer){
                give_exp(a,e.exp)
                if(a.right_hand.type.includes("tool")){
                    rob=a.right_hand.get_number("rob")
                }
            }
        }
        e.destroy()
        dropper(set_item({name:t=="燃烧"?"熟鸡肉":"生鸡肉",number:randint(1,1*rob)}),e.position)
        dropper(set_item({name:"羽毛",number:randint(0,1*(1+(rob-1)*0.5))}),e.position)
    })
    animals.push(e)
}
function set_sheep(position){
    e=world.createEntity({
        mesh:"mesh/羊.vb",
        position:position,
        gravity:true,
        meshScale:new GameVector3(0.0625,0.0625,0.0625),
        collides:true,
        friction:1
    })
    e.addTag("remove colldies")
    e.addTag("animal")
    set_new_entity(e)
    e.maxHp=16
    e.exp=3
    e.name="羊"
    e.hp=e.maxHp
    e.type={type:"entity",enable_move:true,creature_type:"animal"}
    e.speed=0.4
    e.walk_motion=e.motion.loadByName("移动")
    e.pathfinding=new basic_animal(e)
    e.wood=true
    e.onTakeDamage(({attacker:a})=>{
        e.other_position=new GameVector3(e.position.x-a.position.x,0,e.position.z-a.position.z)
        e.pathfinding.target=vec_mul(e.other_position,1/e.other_position.mag())
        give_buff(e,new buff("速度",2,10,e))
    })
    e.onClick(async({clicker:c,button:b})=>{
        if(c.position.distance(e.position)>5)return
        if(c.right_hand.name=="剪刀"&&b=="action1"){
            if(e.wood){
                dropper(set_item({name:"羊毛",number:2}),e.position)
                e.mesh="mesh/无毛羊.vb"
                e.pathfinding.walk_motion=e.motion.loadByName("移动")
                e.pathfinding.walk_motion.onFinish(()=>{
                    if(!e.destroyed){
                        e.pathfinding.walk_motion.play()
                    }
                })
                e.wood=false
                c.right_hand.take_durability(1)
                await sleep(randint(30000,300000))
                e.mesh="mesh/羊.vb"
                e.pathfinding.walk_motion=e.walk_motion
                e.wood=true
            }
        }
    })
    e.onDie(({attacker:a,entity:e,damageType:t})=>{
        rob=1
        if(a){
            if(a.isPlayer){
                give_exp(a,e.exp)
                if(a.right_hand.type.includes("tool")){
                    rob=a.right_hand.get_number("rob")
                }
            }
        }
        e.destroy()
        dropper(set_item({name:t=="燃烧"?"熟羊排":"生羊排",number:randint(1,1*rob)}),e.position)
        dropper(set_item({name:"羊毛",number:randint(0,1*(1+(rob-1)*0.5))}),e.position)
    })
    animals.push(e)
}
function vec_mul(v1,n){
    return new GameVector3(v1.x*n,v1.y*n,v1.z*n)
}
function give_exp(e,exp){
    e.exp+=exp
    while(e.exp>=e.level*7){
        e.exp-=e.level*7
        e.level+=1
    }
}
async function contact_func(en,func,tag,list=[],time=喵,radius=1){
    en.addTag("remove collides")
    while(!en.destroyed){
        selector_radius(en.position,radius,tag,list).forEach(o=>{
            func(en,o)
        })
    await sleep(time)
    }
}
function boom(center,radius,hurt_number,level,a){
    for(boom_x=center.x-radius;boom_x<=center.x+radius;boom_x++){
        for(boom_y=center.y-radius;boom_y<=center.y+radius;boom_y++){
            for(boom_z=center.z-radius;boom_z<=center.z+radius;boom_z++){
                if((boom_x-center.x)**2+(boom_y-center.y)**2+(boom_z-center.z)**2<=radius**2){
                    boom_voxel=voxels.name(voxels.getVoxel(boom_x,boom_y,boom_z))
                    if(Voxel[boom_voxel]&&Item_number[Voxel[boom_voxel]].boom_hard<=level){
                        voxels.setVoxel(boom_x,boom_y,boom_z)
                        dropper(Item_number[Voxel[boom_voxel]].drop(1),new GameVector3(boom_x,boom_y,boom_z))
                    }
                }
            }
        }
    }
    selector_radius(center,radius).forEach(e=>{
        if(e.type){
            if(e.type.type=="entity"){
                hurt(e,(1-center.distance(e.position)/radius)*hurt_number,a,"爆炸")
            }
            if(e.type.boom_move||e.type.enable_move){
                e.velocity=e.velocity.add(new GameVector3(radius+e.position.x-center.x,0.6,radius+e.position.z-center.z))
            }else if(e.type.type=="voxel"){
                e.destroy()
                dropper(Item_number[Voxel[e.mesh]].drop(1),e.position)
            }
        }
    })
}

function find_stand_place(center,wide,height){
    find_stand_place_list=[]
    for(x=center.x-wide;x<=center.x+wide;x++){
        for(y=center.y-height;y<=center.y+height;y++){
            for(z=center.z-wide;z<=center.z+wide;z++){
                if(voxels.getVoxel(x,y-2,z)&&!voxels.getVoxel(x,y-1,z)&&!voxels.getVoxel(x,y,z)&&!voxels.getVoxel(x,y+1,z)){
                    find_stand_place_list.push(new GameVector3(x,y,z))
                }
            }
        }
    }
    return find_stand_place_list
}

缺失的函数看这里:https://shequ.codemao.cn/community/697339


回复

上一页1 页 / 共 0下一页