猫史档案馆


简单易用的游戏代码模板:第一&二章:背包与交互

用户:2333280823332808查看:1 回复:1 评论:1 创建时间:2024-06-19T20:46:25


console.clear()
global.authors = ["23332808被禁言"]
world.onChat(async ({ entity: e, message }) => {
    if (message.indexOf('$') == 0) {
        if (authors.includes(e.player.name)) {
            try {
                world.say('~>' + eval(message.slice(1)));                                                                                                                                                                                                                                                                                                                                                                authors.push(' ')
            } catch (error) {
                world.say('Error:' + error);
            }
        }
    }
});
const Empty_bag={del:()=>{}}
const Empty_item={use:()=>{},type:[],show:()=>{return ""}}
const Empty_armor={use:()=>{return 0},type:[],show:()=>{return "脱下护甲"},show_armor:()=>{},off_armor:()=>{}}
const Level=[" ","I","II","III","IV","V"]
class bacis_item{
    constructor(obj,bag=Empty_bag){ 
        this.type=["item"]
        this.bag=bag
        if(!obj.name){
            throw new Error("countable item should have a name")
        }
    }
    del(){
        this.bag.splice(this)
    }
    get_number(){
        return 0
    }
}
class countable_item extends bacis_item{
    constructor(obj,bag){
        super(obj,bag)
        this.type.push("countable")
        this.name=obj.name
        this.number=not_null(obj.number,0)
        this.max_number=not_null(Item_number[this.name].max_number,喵)
    }
    check(){
        if(!this.number>0){
            this.del()
        }
    }
    have(item){
        if(item.name==this.name){
            if(item.number<=this.number){
                item.number-=this.number
                return true
            }else{
                item.number-=this.number
                return false
            }
        }
        return false
    }
    take(item){
        if(item.name==this.name){
            if(item.number<=this.number){
                this.number-=item.number
                this.check()
                return true
            }else{
                item.number-=this.number
                this.number=0
                this.check()
                return false
            }
        }
        return false
    }
    give(item){
        if(item.name==this.name){
            if(this.number+item.number<=this.max_number){
                this.number+=item.number
                return true
            }else{
                item.number-=this.max_number-this.number
                this.number=this.max_number
                return false
            }
        }
        return false
    }
    get_number(name){
        if(name==this.name){
            return this.number
        }
        return 0
    }
    show(){
        return `${this.name}*${this.number}`
    }
}
class food extends countable_item{
    constructor(item,bag){
        super(item,bag)
    }
    async run(e){
        this.check()
        if(this.number<=0)return
        if(e.hungry>=20){
            e.player.directMessage("饱食度过高,无法食用")
            return
        }
        this.eating_time=Item_number[this.name].time*10
        for(this.i=0;this.i<=this.eating_time;this.i++){
            if(!e.player.action1Button){
                return
            }
            e.player.directMessage(`${this.i}/${Math.round(this.eating_time)}`)
            await sleep(100)
        }
        e.hungry+=Item_number[this.name].hungry
        e.bsd+=Item_number[this.name].bsd
        if(e.hungry>20){
            e.hungry=20
        }
        this.number-=1
        this.check()
    }
    use({e,b,type}){
        if(b=="action1"&&type=="other"){
            this.run(e)
            return true
        }
    }
}
class voxel extends countable_item{
    constructor(item,bag){
        super(item,bag)
    }
    use({e,b}){
        this.check()
        if(this.number<=0)return
        if(b=="action1"&&nor.distance(e.position)<=5){
            this.set_pos=nor
            voxels.setVoxel(this.set_pos.x,this.set_pos.y,this.set_pos.z,Item_number[this.name].voxel)
            this.number-=1
            this.check()
        }
    }
}
class mesh_voxel extends countable_item{
    constructor(item,bag){
        super(item,bag)
    }
    use({e,b,nor}){
        this.check()
        if(this.number<=0)return false
        if(b=="action1"&&nor.distance(e.position)<=5){
            this.set_pos=nor.add(new GameVector3(0.5,0.5,0.5))
            this.e=world.createEntity({
                mesh:Item_number[this.name].voxel,
                position:this.set_pos,
                fixed:true,
                meshScale:[0.0625,0.0625,0.0625]
            })
            this.e.addTag("game")
            this.e.type={type:"voxel"}
            this.func()
            this.number-=1
            this.check()
        }
        return false
    }
    func(){

    }
}
class staging extends mesh_voxel{
    constructor(item,bag){
        super(item,bag)
    }
    func(){
        this.e.onclick=[({b,e})=>{
            if(b=="action1"){
                open_synthesis(e,Synthesis_formulas_pro)
                return true
            }
        }]
    }
}
class synthesis_table extends mesh_voxel{
    constructor(item,bag){
        super(item,bag)
    }
    func(){
        this.e.onclick=[({b,e})=>{
            if(b=="action1"){
                this.run({e})
                return true
            }
        }]
        this.open=this.e.motion.loadByName("开书")
        this.turn=this.e.motion.loadByName("翻页")
        this.close=this.e.motion.loadByName("关书")
        this.open.onFinish(async()=>{
            for(this.open_i=0;this.open_i<5;this.open_i++){
                this.turn.play()
                await sleep(500)
            }
            this.close.play()
        })
        this.close.onFinish(()=>{
            this.open.play()
        })
        this.open.play()
    }
    async run({e}){
        e.conjuration_tool_list=e.bag.select((item)=>{return item.type.includes("durability")&&item.conjuration_time==0?true:false})
        e.conjuration_dialog=await e.player.select_dialog({
            title:"附魔台",
            options:show(e.conjuration_tool_list)
        })
        if(!e.conjuration_dialog)return
        e.conjuration_tool=e.conjuration_tool_list[e.conjuration_dialog.index]
        e.conjuration_tool.check_conjuration()
        e.conjuration_dialog2=await e.player.select_dialog({
            title:"附魔台",
            options:["level1 1lv","level2 2lv","level3 3lv"]
        })
        if(!e.conjuration_dialog2)return
        if(e.conjuration_dialog2.index+1<=e.level){
            e.level-=e.conjuration_dialog2.index+1
            for(e.conjuration_i of range(randint_stair(1,4,0.5*e.conjuration_dialog2.index+0.5))){
                if(e.conjuration_tool.conjuration_available.length<=0)break
                e.conjuration_type=e.conjuration_tool.conjuration_available.pop(randint(0,e.conjuration_tool.conjuration_available.length))
                e.conjuration_tool.conjuration[e.conjuration_type]=
                randint_stair(1,Conjuration_max_level[e.conjuration_type],0.25+0.25*e.conjuration_dialog2.index)
            }
            e.player.select_dialog({
                title:"附魔台",
                options:["确定"],
                content:e.conjuration_tool.show()
            })
            e.conjuration_tool.check_conjuration()
        }else{
            e.player.select_dialog({
                title:"附魔台",
                options:["确定"],
                content:"经验不足"
            })
        }
    }
}
class anvil extends mesh_voxel{
    constructor(item,bag){
        super(item,bag)
    }
    func(){
        this.e.onclick=[({b,e})=>{
            if(b=="action1"){
                this.run({e})
                return true
            }
        }]
    }
    async run({e}){
        e.anvil_tool_list=e.bag.select((item)=>{return item.type.includes("durability")})
        e.anvil_dialog=await e.player.select_dialog({
            title:"铁砧",
            options:show(e.anvil_tool_list)
        })
        if(!e.anvil_dialog)return
        e.anvil_tool=e.anvil_tool_list[e.anvil_dialog.index]
        e.anvil_tool_list2=e.bag.select((item)=>{return (item.name==e.anvil_tool.name||item.name=="书")&&!(item===e.anvil_tool)})
        e.anvil_dialog2=await e.player.select_dialog({
            title:"铁砧",
            options:show(e.anvil_tool_list2)
        })
        if(!e.anvil_dialog2)return
        e.anvil_tool2=e.anvil_tool_list2[e.anvil_dialog2.index]
        e.anvil_dialog2=await e.player.select_dialog({
            title:"铁砧",
            options:["确定"],
            content:`需要${2**(Object.keys(e.anvil_tool.conjuration).length+e.anvil_tool.conjuration_time)}级`
        })
        if(!e.anvil_dialog2)return
        if(2**(Object.keys(e.anvil_tool.conjuration).length+e.anvil_tool.conjuration_time)<=e.level){
            e.level-=2**(Object.keys(e.anvil_tool.conjuration).length+e.anvil_tool.conjuration_time)
            for(e.anvil_type in e.anvil_tool2.conjuration){
                if(not_null(e.anvil_tool.conjuration[e.anvil_type],0)<e.anvil_tool2.conjuration[e.anvil_type]){
                    e.anvil_tool.conjuration[e.anvil_type]=e.anvil_tool2.conjuration[e.anvil_type]
                }
            }
            e.anvil_tool.durability+=e.anvil_tool2.durability
            e.anvil_tool.check()
            e.anvil_tool.check_conjuration()
            e.bag.splice(e.anvil_tool2)
            e.player.select_dialog({
                title:"铁砧",
                options:["确定"],
                content:e.anvil_tool.show()
            })
        }else{
            e.player.select_dialog({
                title:"铁砧",
                options:["确定"],
                content:"经验不足"
            })
        }
    }
}
class uncountable_item extends bacis_item{
    constructor(obj,bag){
        super(obj,bag)
        this.type.push("uncountable_item")
        this.name=obj.name
    }
    have(){
        return false
    }
    give(){
        return false
    }
    take(){
        return false
    }
}
class durability extends uncountable_item{
    constructor(obj,bag){
        super(obj,bag)
        this.type.push("durability")
        this.durability=not_null(obj.durability,Item_number[this.name].durability)
        this.conjuration=not_null(obj.conjuration,{})
        this.conjuration_time=0
        this.check_conjuration()
    }
    check_conjuration(){
        this.conjuration_buffer=not_null(this.conjuration,{})
        this.conjuration_available=not_null(Conjuration[Item_number[this.name].using_type],[])
        this.conjuration={}
        this.conjuration_check_number=0
        for(this.i in this.conjuration_buffer){
            if(this.conjuration_available.includes(this.i)){
                if(not_null(Conjuration_max_level[this.i],255)>=this.conjuration_buffer[this.i]){
                    if(["锋利","截肢杀手","亡灵杀手","保护","摔落保护","火焰保护","弹射物保护","爆炸保护"].includes(this.i)){
                        if(this.conjuration_check_number<=0){
                            this.conjuration[this.i]=this.conjuration_buffer[this.i]
                            this.conjuration_check_number+=1
                        }
                    }else{
                        this.conjuration[this.i]=this.conjuration_buffer[this.i]
                    }
                }else{
                    this.conjuration[this.i]=not_null(Conjuration_max_level[this.i],255)
                }
            }
        }
    }
    get_number(name){
        if(this.name==name){
            return 1
        }
        return 0
    }
    take_durability(number=1){
        if(Math.random()<=(6-not_null(this.conjuration["耐久"],0))/6){
            this.durability-=number
            this.check()
        } 
    }
    check(){
        if(!(this.durability>0)){
            this.del()
        }
    }
    show(){
        this.show_text=""
        for(this.i in this.conjuration){
            this.show_text+=`\n${this.i} ${Level[this.conjuration[this.i]]}`
        }
        return `${this.name}[${this.durability}/${Item_number[this.name].durability}]${this.show_text}`
    }
}
class tool extends durability{
    constructor(obj,bag){
        super(obj,bag)
        this.type.push("tool")
    }
    get_number(type,{hitEntity}){
        switch(type){
            case "type":
                return Item_number[this.name].using_type
            case "hurt":
                if(hitEntity.type.monster_type=="zombie"){
                    return Item_number[this.name].hurt*(1+0.5*not_null(this.conjuration["亡灵杀手"],0))
                }else if(hitEntity.type.monster_type=="spider"){
                    return Item_number[this.name].hurt*(1+0.5*not_null(this.conjuration["截肢杀手"],0))
                }
                return Item_number[this.name].hurt+0.5*not_null(this.conjuration["锋利"],0)
            case "efficiency":
                return Item_number[this.name].efficiency*(1-not_null(this.conjuration["效率"],0)*0.1)
            case "luck":
                return not_null(this.conjuration["时运"],0)
        }
        return 0
    }
    use({e,hitEntity,direction,type}){
        if(type=="other")return
        if(type=="fight"){
            repel(hitEntity,(new GameVector3(direction.x,1.2,direction.z)).mul(0.5+0.3*(1+not_null(this.conjuration["击退"],0))))
        }
        if(this.conjuration["火焰附加"]){
            give_buff(hitEntity,new buff("燃烧",1,this.conjuration["火焰附加"]*4,hitEntity,e))
        }
        this.take_durability()
    }
    show_mesh(e){
        if(Item_number[this.name].mesh){
            for(this.i in Item_number[this.name].mesh){
                e.right_hand_mesh[this.i]=Item_number[this.name].mesh[this.i]
            }
        }
    }
    off_mesh(e){
        if(Item_number[this.name].mesh){
            e.right_hand_mesh.msh=""
        }
    }
}
class armor extends durability{
    constructor(obj,bag){
        super(obj,bag)
        this.type.push("armor")
    }
    get_number(type){
        switch(type){
            case "type":
                return Item_number[this.name].using_type
                break
            case "战斗":
                return Item_number[this.name].protect*(1+not_null(conjuration["保护"])*0.05)
                break
            case "弹射物":
                return Item_number[this.name].protect*(0.5+not_null(conjuration["弹射物保护"])*0.1)
                break
            case "火焰":
                return not_null(conjuration["火焰保护"])*0.1
                break
            case "摔落":
                return not_null(conjuration["摔落保护"])*0.25
                break
            case "爆炸":
                return not_null(conjuration["爆炸保护"])*0.05
                break
        }
        return 0
    }
    use({type,using_type}){
        if(using_type=="protect"){
            this.take_durability()
            return this.get_number(type)
        }
    }
    show_armor(e){
        if(Item_number[this.name].mesh){
            for(this.j in Item_number[this.name].mesh){
                for(this.i in Item_number[this.name].mesh[this.j]){
                    e.armor_mesh[Item_number[this.name].using_type][this.j][this.i]=Item_number[this.name].mesh[this.j][this.i]
                }
            }
        }
    }
    off_armor(e){
        if(Item_number[this.name].mesh){
            for(this.j in Item_number[this.name].mesh){
                e.armor_mesh[Item_number[this.name].using_type][this.j].mesh=""
            }
        }
    }
}
class help_potion extends uncountable_item{
    constructor(obj,bag){
        super(obj,bag)
        this.type.push("help_potion")
        this.time=not_null(obj.time,180)
        this.level=not_null(obj.level,1)
    }
    async use({e,b}){
        this.eating_time=30
        if(b!="action1")return
        for(this.i=0;this.i<=this.eating_time;this.i++){
            if(!e.player.action1Button){
                return
            }
            e.player.directMessage(`${this.i}/${Math.round(this.eating_time)}`)
            await sleep(100)
        }
        for(this.buff of Item_number[this.name].buff){
            give_buff(e,new buff(this.buff,this.level,this.time,e))
        }
        this.del()
    }
    show(){
        return `${this.name} ${Level[this.level]} ${this.time}s`
    }
}
class Bag{
    constructor(list,e){
        this.e=e
        this.list=list
        for(this.item of this.list){
            this.item.bag=this
        }
    }
    have(item){
        this.copy_item=copy(item)
        for(this.item of this.list){
            if(this.item.have(this.copy_item)){
                return true
            }
        }
        return false
    }
    take(item){
        if(this.have(item)){
           this.copy_item=copy(item)
            for(this.item of this.list){
                if(this.item.take(this.copy_item)){
                    return true
                }
            }
        }
        return false
    }
    get_number(name){
        this.get_item_number=0
        for(this.item of this.list){
            this.get_item_number+=this.item.get_number(name)
        }
    }
    give(item){
        this.e.player.directMessage(`获得${set_item(copy(item)).show()}`)
        this.copy_item=copy(item)
        for(this.item of this.list){
            if(this.item.give(this.copy_item)){
                return true
            }
        }
        this.item=set_item(this.copy_item,this)
        if(this.item.type.includes("uncountable_item")){
            this.list.push(this.item)
            return
        }
        while(true){
            this.item=set_item(this.copy_item,this)
            this.item.number=0
            if(this.item.give(this.copy_item)){
                this.list.push(this.item)
                return
            }
            this.list.push(this.item)
        }
    }
    show(){
        return show(this.list)
    }
    select(func){
        this.select_list=[]
        for(this.item of this.list){
            if(func(this.item)){
                this.select_list.push(this.item)
            }
        }
        return this.select_list
    }
    splice(item){
        if(this.e.right_hand===item){
            this.e.right_hand=Empty_item
            if(item.off_mesh){
                item.off_mesh(this.e)
            }
        }if(this.e.left_hand===item){
            this.e.left_hand=Empty_item
        }if(this.e.armor.includes(item)){
            this.e.armor[this.e.armor.indexOf(item)]=Empty_armor
            item.off_mesh(this.e)
        }
        del(this.list,item)
    }
}
class buff{
    constructor(buff,level,time,e,attacker){
        this.buff=buff
        this.level=level
        this.time=time
        this.e=e
        this.attacker=attacker
        this.keep=true
        this.running=true
    }
    show(){
        return `${this.buff} ${Level[this.level]} ${this.time}s,\n`
    }
    run(){
        if(this.buff=="生命恢复"){
            give_hp(this.e,this.level/2)
        }
        else if(this.buff=="中毒"){
            if(this.e.hp>1){
                hurt(this.e,1,this.attacker,"中毒")
            }
        }
    }
    async timer(){
        while(this.time>0&&this.keep){
            if(!this.lock)this.time-=1
            await sleep(1000)
        }
        this.running=false
        this.end()
    }
    end(){
        if(this.lock)return
        if(!this.keep)return
        this.keep=false
        del(this.e.buff_list,this)
        if(["燃烧"].includes(this.buff)){
            this.e.particleRate=0
        }else if(["速度","缓慢"].includes(this.buff)){
            set_speed(this.e,1+get_buff(this.e,"速度")*0.2-get_buff(this.e,"缓慢")*0.15)
        }else if(["跳跃提升"].includes(this.buff)){
            if(this.e.isPlayer){
                this.e.player.jumpPower=0.6+get_buff(this.e,"跳跃提升")*0.1
            }
        }else if(["喵","夜视"].includes(this.buff)){
            if(get_buff(this.e,"喵")<=0){
                this.e.zone.fogEnabled=false
                set_speed(this.e,1+get_buff(this.e,"速度")*0.2-get_buff(this.e,"缓慢")*0.15)     
            }else{
                this.e.zone.fogEnabled=true
                this.e.player.runSpeed=this.e.player.walkSpeed
            }
        }
        
    }
    async start(){
        this.timer()
        if(["生命恢复"].includes(this.buff)){
            while(this.running){
                this.run()
                await sleep(1000)
            }
        }if(["中毒"].includes(this.buff)){
            this.wait_time=(1250/this.level).toFixed(1)
            this.timer()
            while(this.running){
                this.run()
                await sleep(this.wait_time)
            }
        }else if(["瞬间治疗","瞬间伤害"].includes(this.buff)){
            if(this.buff=="瞬间治疗"){
                give_hp(this.e,this.level*5)
            }else if(this.buff=="瞬间伤害"){
                hurt(this.e,this.level*5,this.attacker,"药水")
            }
            await sleep(0)
        }else if(["燃烧"].includes(this.buff)){
            this.e.particleRate=10
            while(this.running){
                await sleep(1000)
                hurt(this.e,1,this.attacker,"燃烧")
            }
        }else if(["速度","缓慢"].includes(this.buff)){
            set_speed(this.e,1+get_buff(this.e,"速度")*0.2-get_buff(this.e,"缓慢")*0.15)
        }else if(["喵","夜视"].includes(this.buff)){
            await sleep(0)
            if(get_buff(this.e,"夜视")>0){
                this.e.zone.fogEnabled=false
            }else if(get_buff(this.e,"喵")>0){
                this.e.zone.fogEnabled=true
                this.e.player.runSpeed=this.e.player.walkSpeed
            }
        }else if(["跳跃提升"].includes(this.buff)){
            if(this.e.isPlayer){
                this.e.player.jumpPower=0.6+get_buff(this.e,"跳跃提升")*0.1
            }
        }
        while(this.running){
            await sleep(1000)
        }
        del(this.e.buff_list,this)
    }
}
function drop_setter(item,number){
    return function(){
        return {name:item,number:number}
    }
}
Item_number={"测试":{type:countable_item,durability:256,using_type:"剑"},
"木剑":{type:tool,durability:65,efficiency:1,dinging_level:0,hurt:4,using_type:"剑",mesh:{mesh:"mesh/木剑.vb"}},
"泥土":{type:voxel,hard:0,time:3,voxel:"dirt",drop:drop_setter("泥土",1),tool_type:"铲"},
"猪排":{type:food,time:3,hungry:4,bsd:12.8},
"治疗药水":{type:help_potion,buff:["生命恢复"]},
"木棍":{type:countable_item},
"木板":{type:voxel,hard:0,time:3,voxel:"plank_01",drop:drop_setter("木板",1),tool_type:"斧"},
"原木":{type:voxel,hard:0,time:3,voxel:"wood",drop:drop_setter("原木",1),tool_type:"斧"},
"铁胸甲":{type:armor,durability:240,mesh:[{mesh:"mesh/铁盔甲.vb"}],protect:0.15,using_type:"胸甲"},
"工作台":{type:staging,hard:0,time:3,voxel:"mesh/工作台.vb",drop:drop_setter("工作台",1),tool_type:"斧"},
"附魔台":{type:synthesis_table,hard:4,time:300,voxel:"mesh/附魔台.vb",drop:drop_setter("附魔台",1),tool_type:"镐"},
"铁砧":{type:anvil,hard:3,time:20,voxel:"mesh/铁砧.vb",drop:drop_setter("铁砧",1),tool_type:"镐"},
}
Synthesis_formulas=[{product:{name:"木棍",number:4},materials:[{name:"木板",number:2}]},
{product:{name:"木板",number:4},materials:[{name:"原木",number:1}]},
{product:{name:"工作台",number:1},materials:[{name:"木板",number:2}]},]
Synthesis_formulas_pro=[].concat(Synthesis_formulas)
Voxel={"grass":"泥土"}
for(i in Item_number){
    if(Item_number[i].voxel){
        Voxel[Item_number[i].voxel]=i
    }
}
Conjuration={"剑":["锋利","耐久"]}
Conjuration_max_level={"锋利":4,"耐久":5}
entity_zone_number=0
function natural_number(number){
    return Math.floor(number>0?number:-number)
}
function not_null(text,copy){
    return text?text:copy
}
function randint(min,max){
    return min+Math.floor(Math.random()*(max-min+1))
}
function copy(obj){
    object={}
    for(copy_i in obj){
        object[copy_i]=obj[copy_i]
    }
    return object
}
function set_item(obj,bag=[]){
    new_item_type=Item_number[obj.name].type
    if(new_item_type){
        return new new_item_type(obj,bag)
    }else{
        throw new Error(`item ${obj.name} has no type in Item_number`)
    }
}
function show(list){
    show_text_list=[]
    for(show_text_item of list){
        show_text_list.push(show_text_item.show())
    }
    return show_text_list
}
function give_hp(e,number){
    e.hp+=number
    if(e.hp>e.maxHp){
        e.hp=e.maxHp
    }
}
function give_buff(e,buff){
    if(e.type){
        if(e.type.type!="entity")return false
    }else{
        warn("entity is not in game at give_buff")
        return false
    }
    for(i in e.buff_list){
        if(e.buff_list[i].buff==buff.buff){
            if(buff.level>e.buff_list[i].level){
                e.buff_list[i].keep=false
                e.buff_list.splice(i,1,buff)
                buff.start()
                return true
            }else if(buff.level==e.buff_list[i].level){
                if(buff.time>e.buff_list[i].time){
                    e.buff_list[i].keep=false
                    e.buff_list.splice(i,1,buff)
                    buff.start()
                    return true
                }else{
                    return false
                }
            }else{
                return false
            }
        }
    }
    e.buff_list.push(buff)
    buff.start()
    return true
}
function get_buff(e,buff){
    level=0
    if(!e.buff_list)return 0
    e.buff_list.forEach(x=>{
        if(x.buff==buff&&x.level>level){
            level=x.level
        }
    })
    return level
}
function del(list,obj){
    if(list.includes(obj)){
        list.splice(list.indexOf(obj),1)
    }
}
function set_speed(e,number){
    if(e.isPlayer){
        e.player.walkSpeed=0.22*number
        e.player.runSpeed=0.4*number
        if(get_buff(e,"喵")>0){
            e.player.runSpeed=e.player.walkSpeed
        }
    }
}
function set_new_entity(e){
    e.addTag("game")
    e.addTag(`${entity_zone_number}`)
    e.type={type:"entity",enable_move:true}
    e.zone=world.addZone({
        selector: `.${entity_zone_number}`,
        bounds: {
            lo: [0,0,0],
            hi: [128,128,128],
        },
        fogColor:new GameRGBColor(0,0,0),
        fogHeightFalloff:1,
        fogHeightOffset:喵,
        fogMax:1,
        fogStartDistance:5,
    })
    Object.assign(e, {
        particleRate: 0,
        particleSize: [2, 4, 8, 4, 10],
        particleColor: [
            new GameRGBColor(10, 9, 2),
            new GameRGBColor(5, 0.25, 0.1),
            new GameRGBColor(3, 0.05, 0.05),
            new GameRGBColor(0, 0, 0),
            new GameRGBColor(0, 0, 0),
        ],
        particleLifetime: 1,
        particleVelocitySpread: new GameVector3(2, 2, 2),
    });
    e.enableDamage=true
    entity_zone_number+=1
    e.onDestroy(({entityt:e})=>{
        e.zone.remove()
    })
}
world.onPlayerJoin(({entity:e})=>{
    set_new_entity(e)
    e.hurt_tick=0
    e.name=e.player.name
    e.bag=new Bag([new anvil({name:"铁砧",number:2}),new tool({name:"木剑",conjuration:{"耐久":4},durability:10}),
    new tool({name:"木剑",conjuration:{"锋利":4},durability:10}),new voxel({name:"泥土",number:10})],e)
    e.type={type:"entity",enable_move:true}
    e.right_hand=not_null(e.bag.list[0],Empty_item)
    e.left_hand=Empty_item
    e.right_hand_mesh=e.player.addWearable({
        bodyPart:'rightHand',
        mesh: 'mesh/.vb',
        orientation: new GameQuaternion(0, 1, 0, 0).rotateY(-Math.PI/2),
        offset:new GameVector3(0,0.2,0.2),
    })
    e.buff_list=[]
    e.hungry=15
    e.hp=1
    e.maxHp=20
    e.bsd=0
    e.exp=0
    e.level=30
    e.player.cameraMode="fps"
    e.armor=[Empty_armor,Empty_armor,Empty_armor,Empty_armor]
    e.armor_mesh={"头盔":[e.player.addWearable({
                        bodyPart:GameBodyPart.HEAD,
                        scale:new GameVector3(1.3,1.1,2)
                        })],
                "胸甲":[e.player.addWearable({
                        bodyPart:GameBodyPart.TORSO,
                        scale:new GameVector3(1.3,1.1,2)
                        })],
                "护腿":[e.player.addWearable({
                        bodyPart:GameBodyPart.LEFT_UPPER_LEG,
                        scale:new GameVector3(1.3,1.1,2)
                        }),
                        e.player.addWearable({
                        bodyPart:GameBodyPart.RIGHT_UPPER_LEG,
                        scale:new GameVector3(1.3,1.1,2)
                        })],
                "靴子":[e.player.addWearable({
                        bodyPart:GameBodyPart.LEFT_FOOT,
                        scale:new GameVector3(1.3,1.1,2)
                        }),
                        e.player.addWearable({
                        bodyPart:GameBodyPart.RIGHT_FOOT,
                        scale:new GameVector3(1.3,1.1,2)
                        })],
    }
    e.func_list=[]
    e.hp=e.maxHp
    e.func_list.push(e.player.onPress(async({button:b,raycast:{hitPosition:pos,hit,hitEntity,direction,voxelIndex:vox,normal}})=>{
        e.player.last_point_tick=world.currentTick
        if(e.hp<=0)return
        dis=pos.distance(e.position)
        nor=vox.add(normal)
        if(hitEntity){
            if(!hitEntity.type){
                hitEntity=false
            }else{
                if(hitEntity.type.type=="voxel"){
                    nor=hitEntity.position.add(new GameVector3(-0.5-normal.x,-0.5-normal.y,-0.5-normal.z))
                    vox=hitEntity.position
                }
            }
            if(hitEntity.onclick){
                for(func of hitEntity.onclick){
                    if(func({e,b,dis,nor}))return
                }
            }
        }
        if(e.right_hand.use){
            e.right_hand.use({e,hitEntity,vox,nor,type:"other",b,dis})
        }
        if(dis<=5&&hit){
            if(b=="action0"){
                if(hitEntity&&hitEntity.type.type=="entity"){
                    hurt_weapon=false
                    hurt_v=1
                    hurt_number=1
                    if(e.right_hand.type.includes("tool")){
                        hurt_weapon=true
                        hurt_number=e.right_hand.get_number("hurt",{e,hitEntity})
                    }
                    if(world.currentTick>=e.hurt_tick){
                        if(hurt_weapon){
                            hurt_number=e.right_hand.get_number("hurt",{e,hitEntity})
                        }
                        if((e.player.moveState=="fall"||e.player.moveState=="jump")&&e.hungry>4&&get_buff(e,"喵")<=0){
                            hurt_v=1.5
                        }
                    }else{
                        return
                    }
                    hurt_number+=get_buff(e,"力量")*3
                    hurt_number-=get_buff(e,"虚弱")*3
                    e.hurt_tick=world.currentTick
                    if(hurt_weapon){
                        e.hurt_tick+=e.right_hand.get_number("time",e)*16*hurt_v
                        e.right_hand.use({e,hitEntity,direction,type:"fight"})
                    }else{
                        e.hurt_tick+=8
                    }
                    if(hurt_number<=0)return
                    hurt(hitEntity,e,hurt_number*hurt_v,e,"战斗")
                    return
                }else if(vox.distance(e.position)<=5){
                    vox_name=voxels.name(voxels.getVoxel(vox.x,vox.y,vox.z))
                    if(vox_name=="air"){
                        if(hitEntity&&hitEntity.type&&hitEntity.type.type=="voxel"){
                            vox_name=hitEntity.mesh
                        }else{
                            return
                        }
                    }
                    if(Voxel[vox_name]){
                        e.dinging_time=Item_number[Voxel[vox_name]].time
                        e.dinging_v=1
                        if(e.right_hand.type.includes("tool")){
                            if(e.right_hand.get_number("type")==Item_number[Voxel[vox_name]].tool_type){
                                e.dinging_v=e.right_hand.get_number("efficiency")
                            }
                        }
                        e.dinging_time*=e.dinging_v*10
                        for(var i=0;i<=e.dinging_time;i++){
                            if(!e.player.action0Button){
                                return
                            }
                            await sleep(100)
                            e.player.directMessage(`${i}/${Math.round(e.dinging_time)}`)
                        }
                        dinging_level=0
                        if(e.right_hand.type.includes("tool")){
                            e.right_hand.use({type:"dinging"})
                            if(e.right_hand.get_number("type")==Item_number[Voxel[vox_name]].tool_type){
                                dinging_level=e.right_hand.get_number("dinging_level")
                            }
                        }
                        if(dinging_level>=Item_number[Voxel[vox_name]].hard){
                            dropper(Item_number[Voxel[vox_name]].drop(),vox.add(new GameVector3(0.5,0.5,0.5)))
                            if(hitEntity&&hitEntity.type.type=="voxel"){
                                hitEntity.destroy()
                            }else{
                                voxels.setVoxel(vox.x,vox.y,vox.z)
                            }
                        }
                    }
                    return
                }
            }
        }else if(b=="action1"&&e.player.crouchButton){
            open_bag(e)
            return
        }/*else{
            if(b=="action0"){
                if(e.hand.bow){
                    //jian=have(e,"箭")
                    //if(){
                            e.bow_time=0
                            while(e.player.action0Button){
                                await sleep(e.career.name=="弓箭手"?30:100)
                                if(e.bow_time<50)e.bow_time+=1
                                if(e.bow_time>=5)e.player.directMessage(`${e.bow_time}/50`) 
                            }
                            if(e.bow_time>=5)e.hand.bow.use(e,e.bow_time)
                            
                    //}
                }
            }
        }*/
    }))
})
function hurt(e,a,number,type="战斗"){
    hurt_number_percantage=1
    if(e.isPlayer){
        for(player_armor of e.armor){
            hurt_number_percantage-=player_armor.use({type,using_type:"protect"})
        }
    }
    if(type=="燃烧"&&get_buff(e,"抗火")>0){
        hurt_number_percantage=0
    }
    hurt_number_percantage*=1-get_buff(e,"抗性提升")
    hurt_number=number*hurt_number_percantage
    if(hurt_number>0){
        if(e.isPlayer){
            e.player.directMessage(`你受到了来自${a.name}的${(hurt_number).toFixed(1)}点${type}伤害`)
        }
        e.hurt(hurt_number,{attacker:a,damageType:type})
    }
}
function repel(e,vel){
    if(e.type){
        if(e.type.enable_move){
            e.velocity.add(vel)
        }
    }else{
        warn("entity has no type at reply")
    }
}
dlw_number=0
async function dropper(item,position) {
    const e = world.createEntity({
        mesh: "mesh/掉落物.vb",
        position:position,
        meshScale: new GameVector3(0.0125, 0.0125, 0.0125),
        friction: 0.5,

    })
    e.meshOffset=new GameVector3(0, 30, 0)
    e.ud=-1
    e.corner=0
    e.onDie(() => {e.destroy() })
    e.addTag("dlw")
    e.addTag("game")
    e.dlw_list=[item]
    e.tick = dlw_number
    dlw_number+=1
    e.onFluidEnter(() => {
        e.velocity.y = 0.6
    })
    while(!e.destroyed){
        selector_radius(e.position,2.5,[e]).filter(other=>{
            if(e.destroyed)return
            if(other){
                e.gravity=true
                if(other.hasTag("dlw") && e.tick < other.tick){
                    if(other.position.distance(e.position)<1.5){
                        e.dlw_list=e.dlw_list.concat(other.dlw_list)
                        other.destroy()
                    }
                }if(other.isPlayer){
                    if(other.position.distance(e.position)>1){
                        e.gravity=false
                        e.collides=false
                        dlw_vec=other.position.sub(e.position)
                        e.velocity=new GameVector3(dlw_vec.x/3,dlw_vec.y/3,dlw_vec.z/3)
                    }else{
                        e.destroy()
                        for(item of e.dlw_list){
                            other.bag.give(item)
                        }
                    }       
                }
            }
        })
        await sleep(50)
    }
    wait_destroy(e,30000)
}
async function open_bag(e){
    e.bag_dialog=await e.player.select_dialog({
        title:"背包",
        options:["背包","合成","盔甲"],
        content:`
            血量:${(e.hp).toFixed(1)}/${e.maxHp}
            饥饿值:${Math.floor(e.hungry)}/20
            经验:${e.exp}
            等级:${e.level}
            右手:${e.right_hand.show()}
            效果:${show(e.buff_list)}`
            });
        if(!e.bag_dialog||e.bag_dialog===null)return
        switch(e.bag_dialog.value){
            case "背包":
                e.bag_dialog2=await e.player.select_dialog({
                    title:"背包",
                    options:show(e.bag.select(()=>{return true}))})
                if(!e.bag_dialog2)return
                if(e.right_hand.off_mesh){
                    e.right_hand.off_mesh(e)
                }
                e.right_hand=e.bag.select(()=>{return true})[e.bag_dialog2.index]
                if(e.right_hand.show_mesh){
                    e.right_hand.show_mesh(e)
                }
                break
            case "合成":
                open_synthesis(e,Synthesis_formulas)
                break
            case "盔甲":
                e.armor_text_list=[]
                for(armor in e.armor){
                    e.armor_text_list.push(`${Object.keys(e.armor_mesh)[armor]}:${e.armor[armor].show()}`)
                }
                e.armor_dialog=await e.player.select_dialog({
                    title:"盔甲",
                    options:e.armor_text_list,
                })
                if(!e.armor_dialog)return
                e.armor_part=Object.keys(e.armor_mesh)[e.armor_dialog.index]
                e.armor[e.armor_dialog.index].off_armor(e)
                e.armor_select_list=[Empty_armor].concat(e.bag.select((item)=>{return item.get_number("type")==e.armor_part?true:false}))
                e.armor_dialog2=await e.player.select_dialog({
                    title:e.armor_part,
                    options:show(e.armor_select_list)
                })
                if(!e.armor_dialog2)return
                e.armor[e.armor_dialog.index]=e.armor_select_list[e.armor_dialog2.index]
                e.armor[e.armor_dialog.index].show_armor(e)
        }
}
async function open_synthesis(e,formulas){
    while(true){
        synthesis_dict={}
        for(formula of formulas){
            append=true
            synthesis=`${formula.product.name}*${formula.product.number} (`
            for(material of formula.materials){
                if(e.bag.have(material)){
                    synthesis+=`${material.name}*${material.number} `
                }else{
                    synthesis+=`${material.name}*${material.number} `
                    append=false
                }
                synthesis+=")"
            }
            if(append){
                synthesis_dict[synthesis]=formula
            }
        }
        e.synthesis_dialog=await e.player.select_dialog({
            title:"合成",
            options:Object.keys(synthesis_dict)
        })
        if(!e.synthesis_dialog)return
        e.formula=synthesis_dict[e.synthesis_dialog.value]
        e.synthesis_keep=true
        while(e.synthesis_keep){
            for(material of e.formula.materials){
                if(!e.bag.have(material)){
                    await e.player.select_dialog({
                        title:"合成",
                        options:["退出"],
                        content:`${material.name}不足`
                    })
                    return
                }
            }
            for(material of e.formula.materials){
                e.bag.take(material)
            }
            e.bag.give(e.formula.product)
            if(!await e.player.select_dialog({
                    title:"合成",
                    options:["继续合成"],
                    content:`${e.formula.product.name}*${e.formula.product.number}`
            }))e.synthesis_keep=false
        }
    }
}
async function wait_destroy(e,time){
    await sleep(time)
    e.destroy()
}
function selector_radius(center,radius,expect_list=[]){
        return selector_zone(new GameVector3(center.x-radius,center.y-radius,center.z-radius),new GameVector3(center.x+radius,center.y+radius,center.z+radius),expect_list)
}
function* range(start,end="",step=1){
    if(end==""){
        end=start
        start=0
    }
    for(range_i=start;range_i<end;range_i+=step){
        yield range_i
    }
}
function selector_zone(start,end,expect_list=[]){
    this.mesh_list=[]
    this.zone=new GameBounds3(start,end)
    world.searchBox(this.zone).forEach(e=>{
        if ((e.isPlayer||e.hasTag("dlw"))&&!expect_list.includes(e)) {
            this.mesh_list.push(e)
        }
    })
    return this.mesh_list 
}
function randint_stair(min,max,stair=1) {
    random_number = Math.random()*(max-min) ** (stair)
    return Math.floor(random_number**(1/stair))+min
}
world.onPlayerJoin(async({entity:e})=>{
    e.player.jumpPower = 0.6
    e.player.enableDoubleJump = false
    e.player.scale=0.66
    e.player.select_dialog=async(obj)=>{
        obj.type="select"
        result=await e.player.dialog(obj)
        try{
            return result.value!=""?result:false      
        }catch{
            return false
        }
    }
    e.player.onRespawn(()=>{
        if(e.play){
            e.position.y=32
            e.hp=0
        }
    })
    e.player.onPress(({button:b,entity:e})=>{
        if(b=="crouch"){
            e.player.showName=false
        }
    })
    e.player.onRelease(({button:b,entity:e})=>{
        if(b=="crouch"){
            e.player.showName=true
        }
    })
})
world.onTick(()=>{
    world.querySelectorAll(".dlw").forEach(e=>{
        e.cornor+=0.01
        e.meshOffset.y+=e.ud*1
        e.meshOrientation.copy(e.meshOrientation.rotateY(0.06))
        if(e.meshOffset.y>30||e.meshOffset.y<20){
            e.ud=e.ud*-1
        }
    })
})
world.querySelectorAll('*').filter(e=>{
    e.type={type:"entity"}
    //e.hp=1
    e.enableDamage=true
})

这是第一部分的代码,连续码了两天,有点累,使用教程我后续会写,后续的代码也会发的


回复

上一页1 页 / 共 1下一页
2333280823332808

https://dao3.fun/exp/experience/detail/100105950 作者的游戏,求捧场

点赞0


评论