Skip to content

k1ic/cv-scripting

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 

Repository files navigation

CryptoVoxels Scripting

零、目的:使地块变得可交互

交互方式:

  • 单人(默认)
  • 多人(地块Owner配置后,通过Grid服务器实现)

一、基础知识

1.1 Feature object

Feature List

image

Feature Location

  • Position
  • Scale
  • Rotation

1.2 Player object(当前玩家)

关键属性

  • player.name => 'captainbenis.eth'
  • player.wallet => '0x2D891ED45C4C3EAB978513DF4B92a35Cf131d2e2'
  • player.uuid => avatar uuid for this instance of the player (player may have multiple tabs open with seperate avatars)

1.3 Scripting Wiki

https://wiki.cryptovoxels.com/en/Scripting/Examples

1.4 vox模型库(100+ vox模型现成可用)

https://wiki.cryptovoxels.com/en/voxel-library

1.5 CV 免费地块

https://www.cryptovoxels.com/account/spaces# image

1.6 CV interactive map

https://benjythebee.github.io/CV_interactive_map/index.html

二、Demo 地址

https://www.cryptovoxels.com/play?coords=S@661W,140S,23.5U

三、示例清单

  1. 围着你转的狗子
  2. 一直跟着你的狗子
  3. 猜数字
  4. Big Big Tree
  5. 一扇门
  6. 井字棋
  7. 陷阱

四、示例详情

4.1 围着你转的狗子

1. 代码

setInterval(() => {
	let p = parcel.getPlayers()[0]
	
	if (!p) {
		return
	}
	
	let v1 = feature.position
	let v2 = p.position
	let angle = Math.atan2(v1.z - v2.z, v1.x - v2.x)
	
	feature.rotation.y = Math.PI / -2 - angle
}, 50)

2. 截图

image image

3. 其他配置

image


4.2 一直跟着你的狗子

1. 代码

//Makes an object grabbable v1.1 - Fayelure

let Distance_to_player=0.5 // How far the object is relative to the player (negative will be behind)
let up_down_constant= -0.65  // Positive object will go higher, negative will go lower. eg: -0.65 places the object where your hands are, approximately. 0 will place the object right above your eyes.
let refresh_rate = 50       // Number of milliseconds. Please avoid a refresh rate < 30 as it could crash the grid.

let clone = false; // change this to false if you want to not clone the object.

//-------------- Do not touch ---------------------------

function moveObject(object,player){ // calculates new position of object

  let spherePos = object.position;
  let playerRotation = player.rotation;

  let xDelta = Distance_to_player*Math.cos(-playerRotation.y+Math.PI/2 + Math.PI);
  let yDelta = Distance_to_player*Math.tan(playerRotation.x);
  let zDelta = Distance_to_player*Math.sin(-playerRotation.y+Math.PI/2 + Math.PI);

      var positionRell = [
    player.position.x - xDelta,
    player.position.y - yDelta +(up_down_constant),
    player.position.z - zDelta
  ];

return positionRell
}

function setPosition(newobject,e){ // refreshes the positions
    newobject.set({position:moveObject(newobject,e.player)})
  if(!newobject.position || !newobject.rotation){
    parcel.removeFeature(newobject)
  }else{
  setTimeout(()=>{
    setPosition(newobject,e)
  },refresh_rate)
  }
}

feature.on('click',e=>{ // On click, create new object and start refreshing the position
let newobject = feature
if(clone){
newobject = parcel.createFeature('megavox')
newobject.set({scale:[feature.scale.x,feature.scale.y,feature.scale.z]})

newobject.set({url:feature._content.url})
newobject.position=feature.position
newobject.rotation=feature.rotation
}

setPosition(newobject,e)

})

2. 截图

image image

3. 其他配置

image


4.3 猜数字

1. 代码

var correct = [6,6,6,6] // your code

let txt = parcel.getFeatureById('correcttxt') //Sign that displays the result

/* ---------------- */

var code = [0,0,0,0]

let b1 =parcel.getFeatureById('b1')
let b2 =parcel.getFeatureById('b2')
let b3 =parcel.getFeatureById('b3')
let b4 =parcel.getFeatureById('b4')
let b5 =parcel.getFeatureById('b5')
let b6 =parcel.getFeatureById('b6')


function hasStarted(element){
  return element==0
}

b1.on('click', e=>{
  console.log('1')
  position = code.findIndex(hasStarted)
  code[position]=1

})

b2.on('click', e=>{

  position = code.findIndex(hasStarted)
  code[position]=2

})

b3.on('click', e=>{

  position = code.findIndex(hasStarted)
  code[position]=3

})

b4.on('click', e=>{

  position = code.findIndex(hasStarted)
  code[position]=4

})

b5.on('click', e=>{

  position = code.findIndex(hasStarted)
  code[position]=5

})

b6.on('click', e=>{

  position = code.findIndex(hasStarted)
  code[position]=6

})

function arraysEqual(arr1,arr2){
  i=arr1.length
  for(i;i--;){
    if(arr1[i] != arr2[i]){
      return false;
    }
  }
  return true;
}

function reset(){
  code=[0,0,0,0]
}

feature.on('click', e=>{
  if(arraysEqual(correct,code)){
    txt.set({text:"Great! Your are right!"})
  }else{
    txt.set({text:"Please try again"})
    reset()
  }

})

2. 截图

image image

3. 其他配置

image


4.4 Big Big Tree

1. 代码

let is_grow = true

feature.on('click',e=>{
  if (is_grow) {
    if (feature.scale.x >= 3.8) {
      is_grow = false
    } else {
      feature.scale.x += 0.2;
      feature.scale.y += 0.2;
      feature.scale.z += 0.2;    
    }    
  } else {
    if (feature.scale.x <= 0.2) {
      is_grow = true
    } else {
       feature.scale.x -= 0.2;
       feature.scale.y -= 0.2;
       feature.scale.z -= 0.2;   
    }
  }
})

2. 截图

image image

3. 其他配置

image


4.5 一扇门

1. 代码

let closed = true

feature.on('click',e=>{
  if(closed){
      feature.rotation.y=0
  }else{
      feature.rotation.y=1.2
  }
  
  closed=!closed
})

2. 截图

image image

3. 其他配置

image


4.6 井字棋

1. 代码

let o = parcel.getFeatureById('nought')
let x = parcel.getFeatureById('cross')

let clones = []
let nought = true

feature.on('click', e => {
  if (clones.length === 9) {
    clones.forEach(c => c.remove())
    clones = []
  }

  let c = (nought ? o : x).clone()
  c.position.copyFrom(e.point)
  clones.push(c)

  nought = !nought
})

2. 截图

image

3. 其他配置

image


4.7 陷阱

1. 代码

let b = parcel.getFeatureById('Trapdoor')

b.set({collidable:true})
let timeNonCollidable = 0.001 // time in seconds

feature.on('click',e=>{
    b.set({collidable:false})
    setTimeout(()=>{b.set({collidable:true})},timeNonCollidable*1000)
})

2. 截图

image

3. 其他配置

image


About

Cryptovoxels Scripting Demo

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published